如何加入或分组数组。例如:
array (size=2)
0 =>
array (size=1)
0 => data0
1 =>
array (size=1)
1 => data1
如何将上面的数组放到下面的数组中?
array (size=1)
0 =>
array (size=2)
0 => data0
1 => data1
这可以在1个索引中加入2个值吗?请帮忙。
答案 0 :(得分:1)
怎么样:
$array1 = array(
array (
'month' => 'January',
'item1' => '120000'
),
array (
'month' => 'February' ,
'item1' => '1'
),
array (
'month' => 'March' ,
'item1' => '5206'
),
array (
'month' => 'April',
'item1' => '0'
),
array (
'month' => 'May' ,
'item1' => '0'
),
array (
'month' => 'June',
'item1' => '0'
),
array (
'month' => 'January' ,
'item2' => '0'
),
array (
'month' => 'February' ,
'item2' => '0'
),
array (
'month' => 'March' ,
'item2' => '5106'
),
array (
'month' => 'April',
'item2' => '0'
),
array (
'month' => 'May',
'item2' => '0'
),
array (
'month' => 'June',
'item2' => '0'
)
);
$resp = call_user_func(function($array1,$keyval){
$response = array();
$count = 0;
foreach($array1 as $arr1){
array_walk($array1, function($item,$index)use($arr1,&$response,$count,$keyval){
if($arr1[$keyval] == $item[$keyval]){
$response[$count] = array_merge($arr1,$item);
}
});
$count++;
}
foreach($response as $key=>$value){
$c = 1;
array_walk($response,function($item,$index)use($value,&$c,&$response,$key,$keyval){
if($item[$keyval] == $value[$keyval]){
if($c > 1){
unset($response[$index]);
}
$c++;
}
});
}
return $response;
},$array1,'month');
var_dump($resp);