Home > Backend Development > PHP Tutorial > Two methods of php array merging_PHP tutorial

Two methods of php array merging_PHP tutorial

WBOY
Release: 2016-07-13 10:35:21
Original
915 people have browsed it

We first give two arrays

Copy the 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 and the plus sign to compare these two arrays

Copy the code The code is as follows:

print_r($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, using array_merge 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, 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

Let’s change the array given earlier

Copy the 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 end of 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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/744324.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...
Related labels:
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