首选说明一下这篇文章有点标题党的嫌疑,我前面写了一篇文章对比了php合并一维数组的三种方式,后来又用这三种方式都来处理了一下多维数组,奇怪的是array_merge和+都没有报错,我就以为他们也能用来处理多维数组。仔细观察了一下结果,才发现这两种方式其实
首选说明一下这篇文章有点标题党的嫌疑,我前面写了一篇文章对比了php合并一维数组的三种方式,后来又用这三种方式都来处理了一下多维数组,奇怪的是array_merge和+都没有报错,我就以为他们也能用来处理多维数组。仔细观察了一下结果,才发现这两种方式其实是来打酱油的。
请看代码
$array1 = array(2,4,"color" => "red"); $array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4); $array3 = array("e","f","red"=>array("little"=>"little",3,5),2,4); $array4 = array("a","b","red"=>array("little"=>"big",4,6),8,9); echo "----------------array_merge---------------".PHP_EOL; print_r(array_merge($array3,$array4)); echo "----------------+++++++++++++++++++++---------------".PHP_EOL; print_r($array3+$array4); echo "----------------array_merge_recursive---------------".PHP_EOL; print_r(array_merge_recursive($array3,$array4));
结果如下
----------------array_merge--------------- Array ( [0] => e [1] => f [red] => Array ( [little] => big [0] => 4 [1] => 6 ) [2] => 2 [3] => 4 [4] => a [5] => b [6] => 8 [7] => 9 ) ----------------+++++++++++++++++++++--------------- Array ( [0] => e [1] => f [red] => Array ( [little] => little [0] => 3 [1] => 5 ) [2] => 2 [3] => 4 ) ----------------array_merge_recursive--------------- Array ( [0] => e [1] => f [red] => Array ( [little] => Array ( [0] => little [1] => big ) [0] => 3 [1] => 5 [2] => 4 [3] => 6 ) [2] => 2 [3] => 4 [4] => a [5] => b [6] => 8 [7] => 9 )
看完代码,我想聪明的看官您已经发现了打酱油的含义:array_merge和+把第一维数组的值都当作一个简单变量,然后用处理一维素组的方式把传进去的素组处理了一遍。
只有array_merge_recursive使用了迭代的方式处理了每一维度的素组。
原文地址:php多位数组合并的三种方式, 感谢原作者分享。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6
Visual web development tools