array_merge 删除一个空字符串

array_merge removes a empty string

在使用array_merge()的时候有一点不明白:

$defaultOptions = [
        'active' => null,
        'activeClass' => 'active',
        'wrapper' => [
            'attributes' => null,
            'templateVars' => null
        ],
        'item' => [
            'hasChildrenClass' => '', // this disappears after array_merge
            'attributes' => null,
            'linkAttrs' => null,
            'templateVars' => null
        ]
    ];

    $options = [
        'active' => [5,3],
        'item' => [
            'attributes' => ['class' => 'test']
        ]
    ];

$options = array_merge($defaultOptions, $options);

$options 的结果是

[
      'active' => [
        (int) 0 => (int) 5,
        (int) 1 => (int) 3,
      ],
      'activeClass' => 'active',
      'wrapper' => [
        'attributes' => null,
        'templateVars' => null,
      ],
      'item' => [
        'attributes' => [
          'class' => 'test',
        ],
      ],
    ]

我不明白为什么 $options['item']['hasChildrenClass'] 在我的结果中消失了?

array_merge()中,如果数组具有相同的字符串键,则后面数组的值将覆盖前面的数组。如果数组有数字键,则后面数组中的值将附加到前一个数组中。 如果数组包含 nullempty 值,该值将被跳过并从合并的数组中删除。

Read more at php.net manual