Home >Backend Development >PHP Tutorial >The difference between array_merge and array+array in php

The difference between array_merge and array+array in php

WBOY
WBOYOriginal
2016-07-25 08:58:241162browse
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);
?>

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)


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