PHP returns all the second levels of the $arr two-dimensional array after merging them
array_merge() Merges the cells of one or more arrays, and the values in one array are appended to the previous array. Returns the resulting array. But it can only merge the first layer, but cannot merge the two-dimensional part of the second-level array I wrote some code and it feels very bad. I hope experts can point out a better method
- $arr=array(
- "0" => array(
- "44" => array("id" => 44,"name" => 'Nine Fish Picture '),
- "45" => array("id" => 45,"name" => 'Koi')
- ),
- "1" => array(
- "49" => array("id" => 49,"name" => 'Baifu Tu'),
- "50" => array("id" => 50,"name" => 'Bat')
- ),
- "2" => array(
- "40" => array("id" => 40,"name" => 'ancient money'),
- "41" => array( "id" => 41,"name" => 'Calabash')
- )
- );
- //Merge all the second levels of the $arr two-dimensional array and return
- 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));
-
- /*
- Get the result
- Array
- (
- [0] => Array
- (
- [id] => 44
- [name] => Nine Fish Diagram
- )
-
- [1] => Array
- (
- [id ] => 45
- [name] => Koi
- )
-
- [2] => Array
- (
- [id] => 49
- [name] => Baifu Tu
- )
-
- [ 3] => Array
- (
- [id] => 50
- [name] => bat
- )
-
- [4] => Array
- (
- [id] => 40
- [name] = > Ancient Coin
- )
-
- [5] => Array
- (
- [id] => 41
- [name] => Gourd
- )
-
- )
-
- */
- ?>
Copy Code
|