筆記國度

在這裡放著一些我自己的筆記

[PHP] array add and merge behave

| Comments

Purpose

Compare between array add array()+array() and array_merge() in PHP.

Code:

$arr = array(
    'a' => 1,
    'b' => 1,
    0 => 1,
) + array(
    'a' => 2,
    'c' => 2,
    0 => 2,
);
var_export($arr);
$arr = array_merge( array(
    'a' => 1,
    'b' => 1,
    0 => 1,
) , array(
    'a' => 2,
    'c' => 2,
    0 => 2,
));
var_export($arr);

Result:

array (
  'a' => 1,
  'b' => 1,
  0 => 1,
  'c' => 2,
)
array (
  'a' => 2,
  'b' => 1,
  0 => 1,
  'c' => 2,
  1 => 2,
)

Comments

comments powered by Disqus