Php merges all Layer 2 elements of the $ arr two-dimensional array and returns

Source: Internet
Author: User
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.


  1. $ Arr = array (
  2. "0" => array (
  3. "44" => array ("id" => 44, "name" => 'snapshot '),
  4. "45" => array ("id" => 45, "name" => 'Koi ')
  5. ),
  6. "1" => array (
  7. "49" => array ("id" => 49, "name" => 'blessing 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" => 'Hulu ')
  13. )
  14. );
  15. // Merge all Layer 2 elements 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. Expected result
  28. Array
  29. (
  30. [0] => Array
  31. (
  32. [Id] => 44
  33. [Name] => 9 Yu figure
  34. )
  35. [1] => Array
  36. (
  37. [Id] => 45
  38. [Name] => Koi
  39. )
  40. [2] => Array
  41. (
  42. [Id] => 49
  43. [Name] => baifutu
  44. )
  45. [3] => Array
  46. (
  47. [Id] => 50
  48. [Name] => bat
  49. )
  50. [4] => Array
  51. (
  52. [Id] => 40
  53. [Name] => guqian
  54. )
  55. [5] => Array
  56. (
  57. [Id] => 41
  58. [Name] => Gourd
  59. )
  60. )
  61. */
  62. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.