php将$arr 二维数组的所有第二层合并后返回
array_merge() 将一个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面。返回作为结果的数组。 但是只能合并第一层,却无法合并二级数组的二维部分 我写了点代码,感觉很不好,希望高手指更好的方法
- $arr=array(
- "0" => array(
- "44" => array("id" => 44,"name" => '九鱼图'),
- "45" => array("id" => 45,"name" => '锦鲤')
- ),
- "1" => array(
- "49" => array("id" => 49,"name" => '百福图'),
- "50" => array("id" => 50,"name" => '蝙蝠')
- ),
- "2" => array(
- "40" => array("id" => 40,"name" => '古钱'),
- "41" => array("id" => 41,"name" => '葫芦')
- )
- );
- //将$arr 二维数组的所有第二层合并后返回
- function array_merge_array($arr)
- {
- $str="$"."son_arr=array_merge(";
- foreach($arr as $k => $v)
- {$str.='$arr['.$k.'],';}
- $str.=");";
- eval(str_replace(',)',')',$str));
- return $son_arr;
- }
- print_r(array_merge_array($arr));
- /*
- 得到结果
- Array
- (
- [0] => Array
- (
- [id] => 44
- [name] => 九鱼图
- )
- [1] => Array
- (
- [id] => 45
- [name] => 锦鲤
- )
- [2] => Array
- (
- [id] => 49
- [name] => 百福图
- )
- [3] => Array
- (
- [id] => 50
- [name] => 蝙蝠
- )
- [4] => Array
- (
- [id] => 40
- [name] => 古钱
- )
- [5] => Array
- (
- [id] => 41
- [name] => 葫芦
- )
- )
- */
- ?>
|