Home >Backend Development >PHP Tutorial >Analyze the difference between array_merge and array+array in php_PHP tutorial

Analyze the difference between array_merge and array+array in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:04:27979browse

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 the same at the same time and must be a string key. merge. And array+array means that no matter what your situation is, 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. If there are more Just add it in, it only counts the number, but in this case it will be added in: $a = array('d'=>'aass','e'=>'adsdfd','asd' =>'asdsdd','ddfg'=>'dssdf');

Copy code The code is as follows:

$b = array('d'=>'adddd','adsdfd','asdfsddddd','d'=>'aass');
$d = $a + $b;
$e = array_merge($a,$b);
var_dump($d);
var_dump($e);

Print:
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)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327779.htmlTechArticlearray_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 the same at the same time...
Statement:
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