Home Backend Development PHP Tutorial PHP returns after merging all the second level of $arr two-dimensional array

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

Jul 25, 2016 am 09:07 AM

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


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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles