Resolve the differences between array_merge and array+array in php

  • 2020-06-19 09:50:43
  • OfStack

array_merge is the key that drops the original number, leaving key as a string, and then forming a new array, regardless of whether the key name is one or not, unless the key name and value are both one and must be key as a string. So array+array is whatever you do, it just puts the previous array in the new array first, and then it sees if the second array has more than the first array, and then it adds more, it only counts, but in this case it adds: $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);

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)

Related articles: