Home > Backend Development > PHP Tutorial > Analysis of the difference between array_merge and array addition in PHP_PHP Tutorial

Analysis of the difference between array_merge and array addition in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:06:40
Original
790 people have browsed it

First let’s see if the key name is string. The difference between the two is:

Copy code The code is as follows:

$arr1 = array('a'=> 'PHP');
$arr2 = array('a'=>'JAVA');
//If the key name is a character and the key name is the same, the array element value after array_merge() will overwrite the previous one Array element value
print_r(array_merge($arr1,$arr2)); //Array ( [a] => JAVA )
//If the key name is a character and the key name is the same, the array addition will Use the first appearing value as the result
print_r($arr1+$arr2); //Array ( [a] => PHP )
?>

If the key name is Numbers, the difference between the two:
Copy code 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 it
print_r(array_merge ($arr1,$arr2));//Array ( [0] => C [1] => PHP [2] => JAVA [3] => PHP )
//If the key name It is an array. When adding arrays, the value that appears first will be used as the result. The subsequent values ​​with the same key names will be discarded
print_r($arr1+$arr2);//Array ( [0] => C [1] = > PHP )
?>

Pay special attention. The "difference between array_merge and array addition" has been explained very clearly above. Returning to my original question "How to most effectively obtain two array value sets with the same character key name and different values"? Isn't this correct? Traverse each array. There is also a function in PHP that you don’t commonly use: array_merge_recursive — recursively merge one or more arrays. If the input arrays have the same string key name, these values ​​​​will be merged into one Go in the array. As an example:
Copy code 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
                                                                                                                                                                                       >)
In this way, you can obtain a set of element values ​​with the same key name in multiple arrays.




http://www.bkjia.com/PHPjc/327601.htmlwww.bkjia.com

truehttp: //www.bkjia.com/PHPjc/327601.htmlTechArticleFirst let’s take a look at the key name is string. The difference between the two: Copy the code 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...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template