php將$arr 二維數組的所有第二層合并後返回

來源:互聯網
上載者:User
php將$arr 二維數組的所有第二層合并後返回

array_merge() 將一個或多個數組的單元合并起來,一個數組中的值附加在前一個數組的後面。返回作為結果的數組。
但是只能合并第一層,卻無法合并二級數組的二維部分
我寫了點代碼,感覺很不好,希望高手指更好的方法


  1. $arr=array(
  2. "0" => array(
  3. "44" => array("id" => 44,"name" => '九魚圖'),
  4. "45" => array("id" => 45,"name" => '錦鯉')
  5. ),
  6. "1" => array(
  7. "49" => array("id" => 49,"name" => '百福圖'),
  8. "50" => array("id" => 50,"name" => '蝙蝠')
  9. ),
  10. "2" => array(
  11. "40" => array("id" => 40,"name" => '古錢'),
  12. "41" => array("id" => 41,"name" => '葫蘆')
  13. )
  14. );
  15. //將$arr 二維數組的所有第二層合并後返回
  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. 得到結果
  28. Array
  29. (
  30. [0] => Array
  31. (
  32. [id] => 44
  33. [name] => 九魚圖
  34. )
  35. [1] => Array
  36. (
  37. [id] => 45
  38. [name] => 錦鯉
  39. )
  40. [2] => Array
  41. (
  42. [id] => 49
  43. [name] => 百福圖
  44. )
  45. [3] => Array
  46. (
  47. [id] => 50
  48. [name] => 蝙蝠
  49. )
  50. [4] => Array
  51. (
  52. [id] => 40
  53. [name] => 古錢
  54. )
  55. [5] => Array
  56. (
  57. [id] => 41
  58. [name] => 葫蘆
  59. )
  60. )
  61. */
  62. ?>
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.