LoginSignup
3
2

More than 5 years have passed since last update.

array_merge_recursive

Posted at

キーが被っていなければ問題が起きることは少ない。

キーが被るとオブジェクトが配列化されるというビックリな挙動する。

array_merge_recursive.php
$obj = new stdClass;
$obj->hoge = 'moge';
$a = array('xxx' => 'val');
$b = array('xxx' => $obj);
$c = array('yyy' => $obj);

var_dump(array_merge_recursive($a, $b));

/*
array(1) {
  'xxx' =>
  array(2) {
    [0] =>
    string(3) "val"
    'hoge' =>
    string(4) "moge"
  }
}
*/

var_dump(array_merge_recursive($a, $c));

/*
array(2) {
  'xxx' =>
  string(3) "val"
  'yyy' =>
  class stdClass#1 (1) {
    public $hoge =>
    string(4) "moge"
  }
}
*/
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2