About the use of PHP array_merge functions

Source: Internet
Author: User

Share the usage of the next Array_merge function.

Merging arrays in PHP is divided into two scenarios:
1. If the two arrays have the same string key name

2. If the two arrays have the same numeric key name

Re-recognize PHP array_merge functions

The

is now re-examining the next Array_merge () function for a bug.
Definition: array_merge-merge one or more arrays
Specification: Array array_merge (array $array 1 [, Array $ ...])
Description:
1. Merges the cells of one or more arrays, and the values in one array are appended to the previous array.
2. If the input array has the same string key name, the value following the key name overrides the previous value. However, if the array contains numeric key names, subsequent values will not overwrite the original values, but are appended to the back.
3. If only one array is given and the array is a numeric index, the key name is re-indexed in a sequential manner.
This function has a detailed example in the manual and is very versatile and useful. (www.yuju100.com)
One of the problems encountered today is the warning in the manual, which was not noticed before, resulting in a fatal error. As follows:
PHP >= version 5.0, Array_merge () only accepts parameters of type array. However, you can use casts to merge other types. The
should be noted for array variables that are generated by code such as foreach, either by initializing the variable to an empty array, or by casting a cast at merge time. Otherwise, there will be a great hardship to eat. Therefore, it is a good thing to keep a habit of initializing variables.
Merge Arrays in PHP into two cases
1, if the two arrays have the same string key name:

<? PHP $book 1 Array (' Linux ' = ' Linux server configuration and Management ', ' php ' = ' php programming '); $book 2 Array (' Linux ' = ' Server configuration and Management ', ' jsp ' = ' PHP '); $result Array_merge ($book 1,$book 2); Print_r ($result);? >

The output is:

Array ([Linux] = server configuration and management [PHP] + PHP programming [JSP] + PHP)

The latter will replace the former. However, if you are using array_merge_recursive (), you can keep it (www.jbxue.com) and make a sub-array exist. Such as:

<? PHP $book 1 Array (' Linux ' = ' Linux server configuration and Management ', ' php ' = ' php programming '); $book 2 Array (' Linux ' = ' Server configuration and Management ', ' jsp ' = ' PHP '); $result array_merge_recursive ($book 1,$book 2); Print_r ($result);? >

The output is:

Array ([Linux] = = Array ([0] = + Linux server configuration and management [1] = + server configuration and Management) [PHP] + PHP programming [JSP] + PHP)

2, if the two arrays have the same numeric key name:

<? PHP $book 1 Array (' Linux server Configuration and Management ', ' PHP programming '); $book 2 Array (' Server Configuration and Management ', ' PHP '); $result Array_merge ($book 1,$book 2); Print_r ($result);? >

The result is:

Array ([0] = + Linux server configuration and management [1] + PHP programming [2] + = Server configuration and management [3] = PHP)

At this point, if the array contains the same numeric key name, then the previous value is not overwritten, but the subsequent key values are incremented sequentially, appended to the back.

About the use of PHP array_merge functions

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.