こんな配列があります
$arr = array(
array('a_1','a_2'),
array('b_1','b_2'),
array('c_1','c_2')
);
最終的には以下のように計算できます。
array(
'a_1,b_1,c_1',
'a_1,b_1,c_2',
'a_1,b_2,c_1',
'a_1,b_2,c_2',
'a_2,b_1,c_1',
' a_2,b_1,c_2',
'a_2,b_2,c_1',
'a_2,b_2,c_2',
); は、配列内の各要素を他の要素と結合します。
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 ))
ありがとう、このアルゴリズムの名前は何ですか?
はデカルト積
と呼ばれます