Introduction to several methods of implementing array merging in PHP

Source: Internet
Author: User

When it comes to array merging, we subconsciously think of Array_merge (), but in our work, a array_merge () function is completely inadequate, so what are the methods that can be used to implement array merging? Today we will take you to learn more about the method of merging PHP arrays!

We introduce to you through an example:

$a = array (' Color ' = ' red ', 5,6); $b = Array (' Color ' = ' blue ', ' type ' = ' fruit ', 6,7); $arr = Array_merge ($a, $b); Var_dump ($arr);
Array (size=6)  ' color ' = = String ' Blue ' (length=4)  0 = int 5  1 = int 6  ' type ' = = String ' Fru It ' (length=5)  2 = int 6  3 = int 7

The requirement is to obtain the same effect without the use of array_merge ();
(Array_merge () merges an array that overrides the key values of the associated form array in the previous array, and the order of the key values in the indexed form is merged together)

1. First use the array_merge_recursive () function to merge:

$a = array (' Color ' = ' red ', 5,6); $b = Array (' Color ' = ' blue ', ' type ' = ' fruit ', 6,7); $arr = Array_merge_recursive ($a, $b); Var_dump ($arr); output: Array (size=6)  ' color ' = =     Array (size=2)          0 = String ' red ' (length=3)          1 = String ' Blue ' (length=4)      0 = int 5  1 = int 6  ' type ' = = String ' fruit ' (length=5)    2 = = Int 6  3 = = int 7

It can be seen from the result that the Array_merge_recursive () function returns the value of the same key value as a new associative array with the key value as the key value of the two-dimensional array, and the other index forms are unaffected.
It is not possible to overwrite the previous value with the previous array key as compared to Array_merge ().

2. Take a look at the ' + ' case of the combined array:

$a = array (' Color ' = ' red ', 5,6); $b = Array (' Color ' = ' blue ', ' type ' = ' fruit ', 6,7); $arr = $a + $b; Var_dump ($arr); Output: Array (size=4)  ' color ' = = String ' Red ' (length=3)  0 = int 5  1 = int 6  ' type ' = str ing ' fruit ' (length=5)

As can be seen from this result, using the ' + ' sign to merge the array, is the front overlay, and Array_merge () opposite, and it is more ruthless than array_merge (), if the contents of the array in the form of an index, after merging if the same key value will be overwritten!

Summarize:

After reading this article believe that many students are suddenly, array merging is not only a kind of, small partners can choose the appropriate PHP array merge method according to their actual project!

Related recommendations:

Example of PHP number combination and de-weight


PHP array Merge Array_merge () function usage considerations


Two ways to merge PHP arrays

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.