The difference analysis of Array_merge and array in PHP _php skill

Source: Internet
Author: User

First look at the key name is string, the difference between the two:

Copy Code code as follows:

<?php
$arr 1 = Array (' A ' => ' PHP ');
$arr 2 = Array (' A ' => ' JAVA ');
If the key name is a character and the key name is the same, the array element value after Array_merge () overrides the preceding array element value
Print_r (Array_merge ($arr 1, $arr 2)); Array ([a] => JAVA)
If the key name is a character and the key name is the same, the addition of the array will result in the first occurrence of the value
Print_r ($arr 1+ $arr 2); Array ([a] => PHP)
?>

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

<?php
$arr 1 = Array ("C", "PHP");
$arr 2 = Array ("JAVA", "PHP");
If the key name is a number, array_merge () does not overwrite
Print_r (Array_merge ($arr 1, $arr 2))//array ([0] => C [1] => PHP [2] => JAVA [3] => PHP)
If the key name is an array, the sum of the arrays will result in the first occurrence of the value, and the same will be discarded after the same key name
Print_r ($arr 1+ $arr 2);//array ([0] => C [1] => PHP)
?>

Special attention should be paid to. On the "difference between the sum of array_merge and Arrays", it is clear that back to my original question "How do I get the most efficient collection of two array values of the same value for the word keys?", not this is going to iterate over each array, PHP also has a function to get you not used: Array_ Merge_recursive-recursively merges one or more arrays, and if the input array has the same string key name, the values are merged into an array. The following example:
Copy Code code as follows:

<?php
$arr 1 = Array ("A" => "PHP", "C");
$arr 2 = Array ("A" => "Java", "C", "Ruby");
Print_r (Array_merge_recursive ($arr 1, $arr 2));
?>

The results are as follows:
Array
(
[A] => Array
(
[0] => PHP
[1] => Java
)
[0] => C
[1] => C
[2] => Ruby
)
This allows you to get a collection of element values that have the same key names in multiple 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.