PHP returns after merging all the second level of $arr two-dimensional array

WBOY
Release: 2016-07-25 09:07:01
Original
863 people have browsed it
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


  1. $arr=array(
  2. "0" => array(
  3. "44" => array("id" => 44,"name" => 'Nine Fish Picture '),
  4. "45" => array("id" => 45,"name" => 'Koi')
  5. ),
  6. "1" => array(
  7. "49" => array("id" => 49,"name" => 'Baifu Tu'),
  8. "50" => array("id" => 50,"name" => 'Bat')
  9. ),
  10. "2" => array(
  11. "40" => array("id" => 40,"name" => 'ancient money'),
  12. "41" => array( "id" => 41,"name" => 'Calabash')
  13. )
  14. );
  15. //Merge all the second levels of the $arr two-dimensional array and return
  16. function array_merge_array($arr)
  17. {
  18. $str="$"."son_arr=array_merge(";
  19. foreach($arr as $k => $v)
  20. {$str.='$arr['.$k.'],';}
  21. $str.=");";
  22. eval(str_replace(',)',')',$str));
  23. return $son_arr;
  24. }
  25. print_r(array_merge_array($arr));
  26. /*
  27. Get the result
  28. Array
  29. (
  30. [0] => Array
  31. (
  32. [id] => 44
  33. [name] => Nine Fish Diagram
  34. )
  35. [1] => Array
  36. (
  37. [id ] => 45
  38. [name] => Koi
  39. )
  40. [2] => Array
  41. (
  42. [id] => 49
  43. [name] => Baifu Tu
  44. )
  45. [ 3] => Array
  46. (
  47. [id] => 50
  48. [name] => bat
  49. )
  50. [4] => Array
  51. (
  52. [id] => 40
  53. [name] = > Ancient Coin
  54. )
  55. [5] => Array
  56. (
  57. [id] => 41
  58. [name] => Gourd
  59. )
  60. )
  61. */
  62. ?>
Copy Code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template