如何将默认值合并到新的多维数组的子数组中?
What is the best way to define a new multidimensional array with default key/value pairs?
I think it's best explained by code sample here:
$defaultOptions = ['foo' => 'bar', 'another' => 'value'];
$mdArray = [
'first' => [
'title' => 'I am first',
$defaultOptions,
],
'second' => [
'title' => 'I am second',
$defaultOptions
]
];
This produces:
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
)
)
)
I would like the 0
key to be omitted from $defaultOptions
in $mdArray
, so that key/value pair would be applied to the same level as where the $defaultOptions
is defined.
Is there a way to do it within array definition, or do I have to process this array later and append these $defaultOptions
?
使用默认键/值对定义新的多维数组的最佳方法是什么? p>
我认为这里最好通过代码示例解释: p>
$ defaultOptions = ['foo'=> 'bar','another'=> 'value'];
$ mdArray = [
'first'=> [
'title'=> '我是第一个',
$ defaultOptions,
],
'cond'=> [
'title'=> '我是第二',
$ defaultOptions
]
];
code> pre>
这会产生: p>
数组
(
[第一个] =>数组
(
[标题] =>我是第一个
[0] =>数组
(
[foo] => bar \ n [另一个] =>值)
)
[n] =>数组
(
[标题] =>我是第二个
[0] =>数组
(
[ foo] => bar
[另一个] =>值
)
)
)
code> pre>
我想 0 要从 $ mdArray code>中的 $ defaultOptions code>中省略的键,以便键/值对应用于 $ defaultOptions 已定义。 p>
有没有办法 o在数组定义中执行此操作,或者我是否必须稍后处理此数组并附加这些 $ defaultOptions code>? p>
div>
You can achieve it in two ways.
The first option is use +
operator:
$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
The second option is use array_merge()
function:
$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.
This can be achieved with array_merge
:
$defaultOptions = ['foo' => 'bar', 'another' => 'value'];
$mdArray = [
'first' => array_merge(['title' => 'I am first'], $defaultOptions),
'second' => array_merge(['title' => 'I am second'], $defaultOptions),
];
You need to use array_merge()
Below is the updated code:
<?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);
?>
And here is the output:
Array
(
[first] => Array
(
[title] => I am first
[foo] => bar
[another] => value
)
[second] => Array
(
[title] => I am second
[foo] => bar
[another] => value
)
)
Purely because there wasn't a loop method with +
posted, I'll add it. This will add the default values to the main array via a reference variable (&$a
).
Code:
$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);
Output:
array (
'first' =>
array (
'title' => 'I am first',
'foo' => 'bar',
'another' => 'value',
),
'second' =>
array (
'title' => 'I am second',
'foo' => 'bar',
'another' => 'value',
),
)
And for the record, array_replace()
will perform this task as well (though it is a less intuitive function to call):
foreach($mdArray as &$a){
$a=array_replace($a,$defaultOptions);
}