如何将默认值合并到新的多维数组的子数组中?

How to merge default values into subarrays of a new multidimensional array?

使用默认 key/value 对定义新的多维数组的最佳方法是什么?

我认为最好用这里的代码示例来解释:

$defaultOptions = ['foo' => 'bar', 'another' => 'value'];
$mdArray = [
    'first' => [
        'title' => 'I am first',
        $defaultOptions,
    ],
    'second' => [
        'title' => 'I am second',
        $defaultOptions
    ]
];

这会产生:

Array 
( 
    [first] => Array 
               ( 
                   [title] => I am first 
                   [0] => Array  
                          ( 
                              [foo] => bar 
                              [another] => value ) 
                          ) 
    [second] => Array 
                ( 
                    [title] => I am second 
                    [0] => Array 
                           ( 
                               [foo] => bar 
                               [another] => value 
                           ) 
                ) 
)

我想从 $mdArray 中的 $defaultOptions 中省略 0 键,这样 key/value 对将应用于与 $defaultOptions 已定义。

有没有办法在数组定义中做到这一点,或者我必须稍后处理这个数组并附加这些 $defaultOptions?

您可以通过两种方式实现它。

第一个选项是使用+运算符:

$mdArray = [
    'first' => ['title' => 'I am first'] + $defaultOptions,
    'second' => ['title' => 'I am second'] + $defaultOptions
];

The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.

http://php.net/manual/en/language.operators.array.php

第二个选项是使用array_merge()函数:

$mdArray = [
    'first' => array_merge(['title' => 'I am first'], $defaultOptions),
    'second' => array_merge(['title' => 'I am second'], $defaultOptions)
];

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended. Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.

http://php.net/manual/en/function.array-merge.php

这可以通过 array_merge 实现:

$defaultOptions = ['foo' => 'bar', 'another' => 'value'];
$mdArray = [
    'first' => array_merge(['title' => 'I am first'], $defaultOptions),
    'second' => array_merge(['title' => 'I am second'], $defaultOptions),
];

您需要使用array_merge()

下面是更新后的代码:

<?php

$defaultOptions = ['foo' => 'bar', 'another' => 'value'];
$mdArray = [
    'first' => [
        'title' => 'I am first'
    ],
    'second' => [
        'title' => 'I am second'
    ]
];

foreach($mdArray as $key => $value){
    $mdArray[$key] = array_merge($mdArray[$key], $defaultOptions);
}

print_r($mdArray);
?>

这是输出:

Array
(
    [first] => Array
        (
            [title] => I am first
            [foo] => bar
            [another] => value
        )

    [second] => Array
        (
            [title] => I am second
            [foo] => bar
            [another] => value
        )

)

纯粹是因为+没有循环方法贴出来,我来补充一下。这将通过引用变量 (&$a).

将默认值添加到主数组

代码:

$defaultOptions=['foo'=>'bar','another'=>'value'];
$mdArray = ['first'=>['title'=>'I am first'],
            'second'=>['title'=>'I am second']
];
foreach($mdArray as &$a){
    $a+=$defaultOptions;
}
var_export($mdArray);

输出:

array (
  'first' => 
  array (
    'title' => 'I am first',
    'foo' => 'bar',
    'another' => 'value',
  ),
  'second' => 
  array (
    'title' => 'I am second',
    'foo' => 'bar',
    'another' => 'value',
  ),
)

郑重声明,array_replace() 也将执行此任务(尽管调用该函数不太直观):

foreach($mdArray as &$a){
    $a=array_replace($a,$defaultOptions);
}