The difference between array_merge and array+array in php

WBOY
Release: 2016-07-25 08:58:24
Original
1111 people have browsed it
This article introduces the difference between array_merge for array merging and array+array for merging. Friends in need, please refer to it.

Instructions: In php, array_merge discards the original numeric key, retains the key in string form, and then forms a new array. Regardless of whether the key names are the same, they will not be merged unless the key name and value are both the same and in string form. The key is merged. But array+array is different. It will only put the data of the previous array into the newly generated array first, and then check whether the second array has more elements than the first array, and if there are more, it will be added. It only counts, but in this case it adds to it. This is the difference between the two.

Look at the example below:

<?php
/**
* array_merge、array+array的区别
* edit bbs.it-home.org
*/
$a = array('d'=>'aass','e'=>'adsdfd','asd'=>'asdsdd','ddfg'=>'dssdf');
$b = array('d'=>'adddd','adsdfd','asdfsddddd','d'=>'aass');
$d = $a + $b;
$e = array_merge($a,$b);
var_dump($d);
var_dump($e);
?>
Copy after login

Output result:

array 'd' => string 'aass' (length=4) 'e' => string 'adsdfd' (length=6) 'asd' => string 'asdsdd' (length=6) 'ddfg' => string 'dssdf' (length=5) 0 => string 'adsdfd' (length=6) 1 => string 'asdfsddddd' (length=10) array 'd' => string 'aass' (length=4) 'e' => string 'adsdfd' (length=6) 'asd' => string 'asdsdd' (length=6) 'ddfg' => string 'dssdf' (length=5) 0 => string 'adsdfd' (length=6) 1 => string 'asdfsddddd' (length=10)


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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!