Difference between the addition of array_merge and array in PHP _ PHP Tutorial

Source: Internet
Author: User
Differences between the addition of array_merge and array in PHP. First, let's take a look at the key name is string, the difference between the two: Copy the code as follows :? Php $ arr1array (aPHP); $ arr2array (aJAVA); if the key name is a character with the same key name, ar first checks whether the key name is string:

The code is as follows:


$ Arr1 = array ('a' => 'php ');
$ Arr2 = array ('a' => 'Java ');
// If the key name is a character with the same key name, the array element value after array_merge () overwrites the previous array element value
Print_r (array_merge ($ arr1, $ arr2); // Array ([a] => JAVA)
// If the key name is a character with the same key name, the first value displayed in the array is used as the result.
Print_r ($ arr1 + $ arr2); // Array ([a] => PHP)
?>


If the key name is a number, the difference is as follows:

The code is as follows:


$ Arr1 = array ("C", "PHP ");
$ Arr2 = array ("JAVA", "PHP ");
// If the key name is a number, array_merge () will not overwrite
Print_r (array_merge ($ arr1, $ arr2 )); // Array ([0] => C [1] => PHP [2] => JAVA [3] => PHP)
// If the key name is an array, the first value in the array will be used as the result, and the values with the same key name will be discarded.
Print_r ($ arr1 + $ arr2); // Array ([0] => C [1] => PHP)
?>


Pay special attention to this. The difference between "array_merge and array addition" is clearly described above, back to my initial question: "How can we get two sets of array values with the same character key name and different values most effectively, it is not commonly used for obtaining: array_merge_recursive-recursively merges one or more arrays. if the input array contains the same string key name, these values will be merged into an array. For example:

The code is as follows:


$ Arr1 = array ("a" => "php", "c ");
$ Arr2 = array ("a" => "java", "c", "ruby ");
Print_r (array_merge_recursive ($ arr1, $ arr2 ));
?>


The result is as follows:
Array
(
[A] => Array
(
[0] => php
[1] => java
)
[0] => c
[1] => c
[2] => ruby
)
In this way, you can obtain a set of element values with the same names in multiple arrays.

The pipeline code is as follows :? Php $ arr1 = array ('a' = 'php'); $ arr2 = array ('a' = 'Java'); // if the key name is a character, and the key name is the same, ar...

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.