如何在PHP中生成多个数组中的项目的所有组合
我试图find几个数组中的项目的所有组合。 数组的数量是随机的(这可以是2,3,4,5 …)。 每个数组中的元素数量也是随机的
例如,我有3个数组:
$arrayA = array('A1','A2','A3'); $arrayB = array('B1','B2','B3'); $arrayC = array('C1','C2');
我想生成一个3 x 3 x 2 = 18组合的数组:
- A1,B1,C1
- A1,B1,C2
- A1,B2,C1
- A1,B2,C2
- A1,B3,C1
- A1,B3,C2
- A2,B1,C1
- A2,B1,C2 …
问题是创build一个具有可变数量的源数组的函数…
这里是recursion解决scheme:
function combinations($arrays, $i = 0) { if (!isset($arrays[$i])) { return array(); } if ($i == count($arrays) - 1) { return $arrays[$i]; } // get combinations from subsequent arrays $tmp = combinations($arrays, $i + 1); $result = array(); // concat each array from tmp with each element from $arrays[$i] foreach ($arrays[$i] as $v) { foreach ($tmp as $t) { $result[] = is_array($t) ? array_merge(array($v), $t) : array($v, $t); } } return $result; } print_r( combinations( array( array('A1','A2','A3'), array('B1','B2','B3'), array('C1','C2') ) ) );
这是一个笛卡儿的产品,我不久前也问过同样的问题 。 这是在PHP网站上发布的algorithm 。
function array_cartesian_product($arrays) { $result = array(); $arrays = array_values($arrays); $sizeIn = sizeof($arrays); $size = $sizeIn > 0 ? 1 : 0; foreach ($arrays as $array) $size = $size * sizeof($array); for ($i = 0; $i < $size; $i ++) { $result[$i] = array(); for ($j = 0; $j < $sizeIn; $j ++) array_push($result[$i], current($arrays[$j])); for ($j = ($sizeIn -1); $j >= 0; $j --) { if (next($arrays[$j])) break; elseif (isset ($arrays[$j])) reset($arrays[$j]); } } return $result; }
我知道这个问题是旧的,但我今天得到同样的问题,并决定给新的发电机尝试:
function generateCombinations(array $array) { foreach (array_pop($array) as $value) { if (count($array)) { foreach (generateCombinations($array) as $combination) { yield array_merge([$value], $combination); }; } else { yield [$value]; } } } foreach (generateCombinations(['a' => ['A'], 'b' => ['B'], 'c' => ['C', 'D'], 'd' => ['E', 'F', 'G']]) as $c) { var_dump($c); }
结果:
array(4) { [0]=> string(1) "E" [1]=> string(1) "C" [2]=> string(1) "B" [3]=> string(1) "A" } array(4) { [0]=> string(1) "E" [1]=> string(1) "D" [2]=> string(1) "B" [3]=> string(1) "A" } array(4) { [0]=> string(1) "F" [1]=> string(1) "C" [2]=> string(1) "B" [3]=> string(1) "A" } array(4) { [0]=> string(1) "F" [1]=> string(1) "D" [2]=> string(1) "B" [3]=> string(1) "A" } array(4) { [0]=> string(1) "G" [1]=> string(1) "C" [2]=> string(1) "B" [3]=> string(1) "A" } array(4) { [0]=> string(1) "G" [1]=> string(1) "D" [2]=> string(1) "B" [3]=> string(1) "A" }
这个代码除了简单之外,还可以获得多个数组的所有组合,并保留键。
function get_combinations($arrays) { $result = array(array()); foreach ($arrays as $property => $property_values) { $tmp = array(); foreach ($result as $result_item) { foreach ($property_values as $property_key => $property_value) { $tmp[] = $result_item + array($property_key => $property_value); } } $result = $tmp; } return $result; }
例:
Array ( Array ( '1' => 'White', '2' => 'Green', '3' => 'Blue' ), Array ( '4' =>' Small', '5' => 'Big' ) )
将返回:
Array ( [0] => Array ( [1] => White [4] => Small ) [1] => Array ( [1] => White [5] => Big ) [2] => Array ( [2] => Green [4] => Small ) [3] => Array ( [2] => Green [5] => Big ) [4] => Array ( [3] => Blue [4] => Small ) [5] => Array ( [3] => Blue [5] => Big ) )