Home > Backend Development > PHP Tutorial > In-depth analysis of the difference between using the plus sign and using array_merge to merge arrays in PHP_PHP Tutorial

In-depth analysis of the difference between using the plus sign and using array_merge to merge arrays in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:09:52
Original
866 people have browsed it

We first give two arrays

Copy code The code is as follows:

$r = array (1,2,3,4,5,6);
$e = array(7,8,9,10);
?>

Below we use array_merge Why bother with these two arrays
Copy code The code is as follows: r+e); // OutputArray ( [0] => 1 [1] => 2 [2] => ; 3 [3] => 4 [4] => 5 [5] => 6 )
print "
";
print_r(array_merge($ r,$e)); // OutputArray ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 )
?>


As you can see from here, array_merge is used to merge arrays. The values ​​in one array are appended to the previous array. Returns the resulting array. If the array contains numeric key names, subsequent values ​​will not overwrite the original values, but will be appended to them. However, use the plus sign to merge arrays. If the key names are the same, the array value that appears first will be taken, and the rest will be ignored
Let’s change the array given earlier


Copy code
The code is as follows: $r = array('r'=>1,2,3,4,5, 6);
$e = array('r'=>7,8,9, 10);
?>




Copy code
The code is as follows: print_r($r+e); // Output Array ( [r] => 1 [0] => 2 [1] => 3 [2] => 4 [3] => ; 5 [4] => 6 )
print "
";
print_r(array_merge($r,$e)); // Output Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [ 8] => 9 )
?>


As you can see from here, array_merge is used to merge arrays. The values ​​in one array are appended to the previous array. If the non-numeric key names are the same, the value of the subsequent array will overwrite the value of the previous array. However, if you use the plus sign to merge arrays, if the key names are the same, the array value that appears first will be taken, and the subsequent ones will be ignored



http://www.bkjia.com/PHPjc/327214.html

www.bkjia.com

http: //www.bkjia.com/PHPjc/327214.htmlTechArticleWe first give two arrays to copy the code as follows: ?php $r = array(1,2,3 ,4,5,6); $e = array(7,8,9,10); ? Below we use array_merge and the plus sign to copy the code for these two arrays...
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