PHP,使用所有其他数组值

时间:2014-12-18 12:40:18

标签: php arrays function combiners

我必须使用其他数组值创建一个复杂的数组。

原始数组是:

Array (
     [0] => Array (
        [0] => A
        [1] => B
        [2] => C
    )

    [1] => Array (
        [0] => D
        [1] => E
        [2] => F
    )
)

我正在寻找能够为我提供所有组合值的函数,例如:

Array (
  [0] => Array (A, D),
  [1] => Array (A, E),
  [2] => Array (A, F),
  [3] => Array (B, D),
  [4] => Array (B, E),
  [5] => Array (B, F),
  [6] => Array (C, D),
  [7] => Array (C, E),
  [8] => Array (C, F)
)

当然,即使原始数组中的值数不同,这个算法仍然可以工作。

我正在寻找PHP文档中的一个简单函数来执行此操作,但我找不到。

我用"多路复用"等关键字搜索了它。或"结合"但没有运气。我认为最好的方法是制作一个递归函数,但我找不到正确的算法。

4 个答案:

答案 0 :(得分:3)

计算笛卡尔积的简单递归函数:

function cartesian(array $data)
{
    function cartesian_r(array $data, array $path, &$out)
    {
        if (count($data) > 1) {
            // recursive step
            $head = array_slice($data, 0, -1);
            $tail = end($data);
            foreach ($tail as $item) {
                cartesian_r($head, array_merge($path, [$item]), $out);
            }
        } else {
            // unwind step
            foreach ($data[0] as $item) {
                $out[] = array_merge([$item], $path);
            }
        }
    }

    $out = [];
    cartesian_r($data, [], $out);

    return $out;
}

$out = cartesian([['A', 'B'], ['C', 'D'], ['E', 'F']]);

print_r($out);

输出

Array
(
    [0] => Array
        (
            [0] => A
            [1] => E
            [2] => C
        )

    [1] => Array
        (
            [0] => B
            [1] => E
            [2] => C
        )

    [2] => Array
        (
            [0] => A
            [1] => E
            [2] => D
        )

    [3] => Array
        (
            [0] => B
            [1] => E
            [2] => D
        )

    [4] => Array
        (
            [0] => A
            [1] => F
            [2] => C
        )

    [5] => Array
        (
            [0] => B
            [1] => F
            [2] => C
        )

    [6] => Array
        (
            [0] => A
            [1] => F
            [2] => D
        )

    [7] => Array
        (
            [0] => B
            [1] => F
            [2] => D
        )

)

答案 1 :(得分:2)

只有两个foreach

$first = array('A', 'B', 'C', 'D');
$second = array('E', 'F', 'G');

$result = array();
foreach ($first as $f) {
    foreach ($second as $s) {
        $result[] = array($f, $s);
    }
}
var_dump($result);

答案 2 :(得分:1)

这适用于你不会使用的子数组。它不是作为原始输出排序的,但可以使用排序函数轻松修复,如下例所示:

 $input = array(
    0 => array ('A','B','C'),
    1 => array ('D','E','F'),
    2 => array ('X','Y','Z'),
    /*...*/
);

$result = array();  
foreach($input as $sub_arr){
    $_result = array();
    foreach($sub_arr as $val){
        if($result){
            foreach($result as $old){
                $old[] = $val;
                $_result[] = $old;
            }
        }else{ 
            $_result[] = array($val);
        }
    }
    $result = $_result;
} 
array_multisort($result); //sort all layers alphabetically.
echo "<pre>";
print_r($result);

工作示例:Fiddle

答案 3 :(得分:0)

这是我能想象到的最简单的动态:

显示&#34;所有组合可能&#34;

<?php

$origArray = array(
    array(
         "A",
         "B",
         "C"        
    ),
     array(
        "D",
        "E",
        "F"     
    )
);

$endArray = array();

foreach ($origArray as $key_1 => $value_1){
    foreach ($origArray as $key_2 => $value_2){
        if($key_1 == $key_2){
            continue;
        }
        foreach($value_1 as $key_1_1 => $value_1_1 ){
            foreach ($value_2 as $key_2_1 => $value_2_1){
                $endArray[] = array($value_1_1, $value_2_1);
            }
        }


    }

}
echo "<pre>";
var_dump($endArray);
echo "</pre>";

如果你想显示你可以使用的第一个子阵列的所有组合:

<?php

$origArray = array(
    array(
         "A",
         "B",
         "C"        
    ),
     array(
        "D",
        "E",
        "F"     
    )
);

$endArray = array();

foreach ($origArray as $key_1 => $value_1){
    foreach($origArray[0] as $key_1_1 => $value_1_1 ){
        if($key_1 == 0 ){
            continue;
        }
        foreach ($value_1 as $key_2_1 => $value_2_1){

            $endArray[] = array($value_1_1, $value_2_1);
        }
    }
}
echo "<pre>";
var_dump($endArray);
echo "</pre>";