开发者

PHP - counting problem algorithm

I need to get all the combinations and permutations of an array of array of values. See snippet for example:

$a = array(
    1,
    2
);
$b = array(
    'foo',
    'bar'
);

$param开发者_高级运维s   = array();
$params[] = $a;
$params[] = $b;

// What to do to $params so I can get the following combinations/permutations?
// 1,foo
// 2,foo
// 1,bar
// 2,bar
// foo,1
// bar,1
// foo,2
// bar,2

Keep in mind that the $params can be any size and the items in it can also be any size.


function all($array, $partial, &$result) {
    if ($array == array()) {
        $result[] = implode(',', $partial);
        return;
    }
    for($i=0; $i<count($array);$i++) {
        $e = $array[$i];
        $a = $array;
        array_splice($a, $i, 1);
        foreach($e as $v) {
            $p = $partial;
            $p[] = $v;
            all($a, $p, $result);
        }
    }
}

Test:

$a = array(1, 2, 3);
$b = array('foo', 'bar');
$c = array('a', 'b');
$params = array($a, $b, $c);

$result = array();
all($params, array(), $result);
print_r($result);

Note: if there is a chance for duplicates (arrays contain the same values) you can check for duplicates before inserting into the $result.


Here is my solution. It should work with any number of associative arrays, even if they contained nested associative arrays.

<?php
    $a = array(1,2,3);
    $b = array('foo','bar','baz', array('other','other2',array('other3','other4')));
    $params = array();
    $params[] = $a;
    $params[] = $b;

    $elements = array();

    foreach($params as $param) {
     addElement($param,$elements);
    }

    function addElement($arg,&$result) {
        if(!is_array($arg)) {
            $result[] = $arg;
        } else {
            foreach($arg as $argArray) {
                addElement($argArray,$result);
            }
        }
    }

    for($i=0; $i<count($elements); $i++) {
        $curElement = $elements[$i];
        for($j=0; $j<count($elements); $j++) {
            if($elements[$j] != $curElement) {
                $final_results[] = $curElement.','.$elements[$j];
            }
        }
    }
    print_r($final_results);

?>

http://codepad.viper-7.com/XEAKFM


Here's a possible solution codepad...

$array = array(
    0   => array(
        'foo',
        'bar'
    ),
    1   => array(
        1,
        2,
        3
    ),
    2   => array(
        'x',
        'y',
        'z'
    ),
    3   => array(
        7,
        8,
        9
    )
);

array_permutations($array, $permutations);
print_r($permutations);

public function array_permutations($array, &$permutations, $current_key = 0, $current_subkey = 0)
{   
    if(!isset($array[$current_key][$current_subkey]))
    {
        return;
    }

    $current_val = $array[$current_key][$current_subkey];

    foreach($array as $array_key => $row)
    {
        foreach($row as $row_key => $sub_val)
        {
            if($array_key === $current_key)
            {
                if($row_key !== $current_subkey)
                {
                    $permutations[] = $current_val . ', ' . $sub_val;
                }
            }
            else 
            {
                $permutations[] = $current_val . ', ' . $sub_val;
            }
        }
    }

    $next_key = ($current_subkey == (count($array[$current_key]) - 1)) ? $current_key + 1 : $current_key;
    $next_subkey = ($next_key > $current_key) ? 0 : $current_subkey + 1;
    array_permutations($array, $permutations, $next_key, $next_subkey); 
}


Try this out, hope this helps

$a = array(
    1,
    2,
    4
);
$b = array(
    'foo',
    'bar',
    'zer'
);
$c = array(
    'aaa',
    'bbb'
);

$params   = array();
$params[] = $a;
$params[] = $b;
$params[] = $c;

$sizeofp = count($params);
for($i=0 ; $i < $sizeofp ; $i++){
    foreach ($params[$i] as $paramA) {
        for($j=0 ; $j < $sizeofp ; $j++){
            if($j == $i){
                continue;
            }
            foreach($params[$j] as $paramB){
                echo "\n".$paramA.",".$paramB;  
            }
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜