时间:2021-07-01 10:21:17 帮助过:3人阅读
function foo($d) { $r = array_pop($d); while($d) { $t = array(); $s = array_pop($d); if(! is_array($s)) $s = array($s); foreach($s as $x) { foreach($r as $y) $t[] = array_merge(array($x), is_array($y) ? $y : array($y)); } $r = $t; } return $r;}$ary = array( 'a'=>array('a_1','a_2'), 'b'=>array('b_1','b_2'), 'c'=>array('c_1','c_2'),);print_r(foo($ary));
Array( [0] => Array ( [0] => a_1 [1] => b_1 [2] => c_1 ) [1] => Array ( [0] => a_1 [1] => b_1 [2] => c_2 ) [2] => Array ( [0] => a_1 [1] => b_2 [2] => c_1 ) [3] => Array ( [0] => a_1 [1] => b_2 [2] => c_2 ) [4] => Array ( [0] => a_2 [1] => b_1 [2] => c_1 ) [5] => Array ( [0] => a_2 [1] => b_1 [2] => c_2 ) [6] => Array ( [0] => a_2 [1] => b_2 [2] => c_1 ) [7] => Array ( [0] => a_2 [1] => b_2 [2] => c_2 ))
感谢,这个算法叫什么名来的?
叫做 笛卡尔积