两个数组合并
不太会描述,我举个例吧
array1
array1[0] = '1';
array1[1] = '2';
array2
array2[0] = '3';
array2[1] = '4';
想组合成
array[0]['arr1'] = '1'
array[0]['arr2'] = '3'
array[1]['arr1'] = '1'
array[1]['arr2'] = '4'
array[2]['arr1'] = '2'
array[2]['arr2'] = '3'
array[3]['arr1'] = '2'
array[3]['arr2'] = '4'
------解决思路----------------------
这是在求 笛卡尔积
------解决思路----------------------
用递归实现的,但是只能是两个数组,感觉这种方法有点笨
<br />$arr1=array(1,2);<br />$arr2=array(3,4);<br /><br />$res = test($arr1,$arr2,0,0);<br /><br />echo "<pre class="brush:php;toolbar:false">";<br />print_r($res);<br />echo "
<br />$arr1 = array(1,2);<br />$arr2 = array(3,4);<br />$arr = array();<br /><br />for($i=0,$max1=count($arr1); $i<$max1; $i++){<br /> for($j=0,$max2=count($arr2); $j<$max2; $j++){<br /> $tmp = array();<br /> $tmp['arr1'] = $arr1[$i];<br /> $tmp['arr2'] = $arr2[$j];<br /> $arr[] = $tmp;<br /> }<br />}<br /><br />print_r($arr);<br />