PHP Array Merging

Source: Internet
Author: User

Many times we are merging multiple arrays, and merging functions in the PHP library is:

Direct merger: array_merge,array_merge_recursive;

Indirect merging: Array_map,array_filter and other methods that can use anonymous functions

Another way to merge is that this is actually an operation, but in many cases it is possible to complete an array merge that cannot be completed directly by merging.

The indirect merge is not described here because the indirect data is already similar to the Foreach Loop processing.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/57/23/wKiom1SSZ7Ow4sV-AAJdJwbCYeg922.jpg "style=" float: none; "title=" qq20141218133356.jpg "alt=" Wkiom1ssz7ow4sv-aajdjwbcyeg922.jpg "/>

The Array_merge_recursive function is merged for multiple arrays but cannot lose every element

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/57/23/wKiom1SSZ7Oj3NCYAAIUeaqurfw488.jpg "style=" float: none; "title=" QQ picture 20141218133311.jpg "alt=" Wkiom1ssz7oj3ncyaaiueaqurfw488.jpg "/>

The Array_merge function is for multiple array merges as long as there are duplicates on the front of the overlay.


Array_merge_recursive,array_merge These two functions are not overwritten when a numeric subscript or index subscript is merged, but instead are overridden by the default index to append to the merged new array in the order of each array element. So when you need to overwrite the elements of the number subscript in the merged array, the two functions cannot be merged directly, either by modifying the subscript to an impure number, by using a looping statement, or by using the +

+ Operations when working with arrays, it is important to ensure that the operands on both sides are arrays, and Note that the preceding array overrides the value of the same key name in the following array, which is exactly the opposite of the function call .

For example:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/57/33/wKiom1ST6JWwnfyzAAGU3xw4nYQ281.jpg "title=" Qq20141219162733.png "alt=" Wkiom1st6jwwnfyzaagu3xw4nyq281.jpg "/>

$arr and $ARR1 are two mixed arrays, the same structure, but the data some differences, through the above different ways to merge the results are not the same, the main aspect of the merger is the array of test2 and 123 of the merge result changes.


The function Array_merge merges with the overlay element, but for the indexed element it will only accumulate instead of overwrite, and the result of the merge is:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/57/30/wKioL1ST6WijMsEaAAFPATZPA9c497.jpg "title=" Qq20141219163102.png "alt=" Wkiol1st6wijmseaaafpatzpa9c497.jpg "/>


Conclusion: using the Array_merge function to merge the array, the system will only be judged by the first subscript of the array, the elements of the digital subscript are all preserved, and the subscript is reset without affecting the index of the element, the order of the marks is starting from the first array and then accumulating the following array. The string class subscript system only retains the last occurrence of the subscript and element, not recursively into the inner array of the element, directly overwriting the preceding element.

The function array_merge_recursive merges with the overlay element, but for the indexed element it will only accumulate instead of overwrite, and the result of the merge is:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/57/30/wKioL1ST6Y_hdLusAAGjnpZfLGg416.jpg "title=" Qq20141219163102.png "alt=" Wkiol1st6y_hdlusaagjnpzflgg416.jpg "/>

Conclusion: using array_merge_recursive array to merge, the system only resets the elements of the first level subscript to index subscript and retains the original position, the string class subscript element is not overwritten, but recursively to the inner layer, Recursion in which there is a number under the cursor stops the current element recursively resets the subscript, subscript is based on the current recursive layer in the first digit subscript as the starting value to accumulate, not all from 0 to accumulate, recursive layer within the inner layer of the string subscript appears at the same time, will create an array in the minimum layer and in a repeating order to add to this array, Arrays are added with the default index subscript

Using $arr + $arr 1 operation is not reset subscript, so the same subscript element will be the + left array to cover the + right element, the result of the operation is:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/57/33/wKiom1ST6RegyqE7AAEBqChG0DQ558.jpg "title=" Qq20141219163102.png "alt=" Wkiom1st6regyqe7aaebqchg0dq558.jpg "/>

Conclusion: using the + operation to merge an array, the system only carries out strict subscript merging of the first element of the operand array, the preceding array overrides all subsequent elements of the same subscript, and does not recursively go to the inner layer, and all numeric subscripts and string subscripts are overwritten, and the subscript is not modified.


Most of the development uses functions or loops to combine arrays, and many other times it's easier to use +, summarizing the three ways of merging scenarios:


Array_merge : applies to the string subscript only (the element that appears before the overlay), the total sum of the subscript is not concerned about whether the subscript is modified or requires resetting the default numeric subscript, all merges are done only in the first level element and the corresponding subscript, and not recursively to the inner element.


array_merge_recursive : applies without overwriting any element, the element with the same string subscript recursively moves to the inner layer and creates an array when the smallest merge layer appends the repeating element to the array, and the number below the cursor stops the current element recursively merges all the current layers (if the first layer resets the number subscript to the default index, The second and deeper layers do not modify the subscript value of the first element, only resets the second and later all when the layer number subscript takes the first subscript as the starting subscript value accumulation) and does not care whether the subscript is modified or requires resetting the default numeric subscript.


+ : The application is applied strictly by the subscript (does not differentiate whether the subscript is overwritten with a number or a string), or when the subscript is not the same, and does not reset the subscript.


This article is from the "PHP in the Web" blog, so be sure to keep this source http://php2012web.blog.51cto.com/5585213/1591933

PHP Array Merging

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.