PHP array merging method

Source: Internet
Author: User
: This article describes how to merge arrays in PHP. For more information about PHP tutorials, see. 1. +

When the key names of two arrays are numeric or string keys, you can directly use "+", $ c = $ a + $ B, append the key name and value after $ a (the key name that $ B does not exist in $.
Features:
Does not overwrite, but append a key that does not exist and the corresponding value
Key names are not re-indexed

No matter whether it is all numeric keys or mixed keys, only append keys and values. if the key names are the same, no append is performed. that is, the first value is returned as the final result.

2. array array_merge (array arra1 [, array array2 [, array...])

Array_merge () combines the units of one or more arrays. the values in an array are appended to the previous array and used as the array of the returned results.

If the input array contains the same string key name, the value after the key name overwrites the previous value.

However, if the array contains a number key name, the subsequent values will not overwrite the original values, but will be appended to the back.

If only an array is assigned and the array is indexed by number, the key name is re-indexed continuously.

Features:

Number index, not covered. after the value is merged, the key name will be re-indexed continuously

String key name, the value after the key will overwrite the previous value

3. array array_merge_recursiv (array array1 [, array...])

Array_merge_recursive () combines the units of one or more arrays. the values in an array are appended to the back of the previous array and returned as the result array.

If the input array has the same string key name, these values will be merged into an array and recursive. Therefore, if a value is an array, this function combines the response entries into another array.

However, if the array has the same array key name, the next value will not overwrite the original value, but will be appended

Features:

The rule is basically the same as that of array_mege. it only uses recursive append when processing the same string key name.

The above describes how to merge arrays in PHP, including some content. I hope my friends who are interested in PHP tutorials can help me.

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.