Php merges all Layer 2 elements of the $ arr two-dimensional array and returns
Php merges all Layer 2 elements of the $ arr two-dimensional array and returns Array_merge ()Combine the elements of one or more arrays. the values of an array are appended to the values of the previous array. Returns an array of results. However, only the first layer can be merged, but the two-dimensional part of the second-level array cannot be merged. I wrote some code, and I don't feel very good. I hope high fingers can provide better methods.
- $ Arr = array (
- "0" => array (
- "44" => array ("id" => 44, "name" => 'snapshot '),
- "45" => array ("id" => 45, "name" => 'Koi ')
- ),
- "1" => array (
- "49" => array ("id" => 49, "name" => 'blessing Tu '),
- "50" => array ("id" => 50, "name" => 'bat ')
- ),
- "2" => array (
- "40" => array ("id" => 40, "name" => 'ancient money '),
- "41" => array ("id" => 41, "name" => 'Hulu ')
- )
- );
- // Merge all Layer 2 elements 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 ));
- /*
- Expected result
- Array
- (
- [0] => Array
- (
- [Id] => 44
- [Name] => 9 Yu figure
- )
- [1] => Array
- (
- [Id] => 45
- [Name] => Koi
- )
- [2] => Array
- (
- [Id] => 49
- [Name] => baifutu
- )
- [3] => Array
- (
- [Id] => 50
- [Name] => bat
- )
- [4] => Array
- (
- [Id] => 40
- [Name] => guqian
- )
- [5] => Array
- (
- [Id] => 41
- [Name] => Gourd
- )
- )
- */
- ?>
|