我有一个数组
$array = array(
0 => array('a1', 'a2'),
1 => array('b4', 'b3', 'b5'),
2=> array('c1', 'c3'),
3=> array('d2' , 'd5', 'd6')
);
我想将数组处理为以下程序:
$data= array();
$tmp = array();
foreach($array[0] as $arr0){
$tmp[0]= $arr0;
foreach($array[1] as $arr1){
$tmp[1]= $arr1;
foreach($array[2] as $arr2){
$tmp[2]= $arr2;
foreach($array[3] as $arr3){
$tmp[3]= $arr3;
$data[]= $tmp;
}
}
}
}
print_r($data);
那么如何对这个程序使用递归?。
答案 0 :(得分:1)
试试这个,查看 live demo 。
$result = [];
$temp = [];
foreach($array as $arr)
{
foreach($arr as $v)
{
if($result == [])
$temp[] = [$v];
else{
foreach($result as $val)
{
$val [] = $v;
$temp[] = $val;
}
}
}
$result = $temp;
$temp = [];
}
print_r($result);