array_replace
使用传递的数组替换第一个数组的元素
&reftitle.description;
arrayarray_replace
arrayarray
arrayreplacements
array_replace 创建新数组,并为提供的数组中的每个 key
都分配元素。如果某个 key 出现在多个输入数组中,则将使用最右侧输入数组中的值。
array_replace 不会递归处理元素项,它在替换时会替换每个 key 的值。
&reftitle.parameters;
array
替换该数组的值。
replacements
包含要提取元素的数组。
后面的数组里的值会覆盖前面的值。
&reftitle.returnvalues;
返回 array。
&reftitle.examples;
array_replace 示例
"pineapple", 4 => "cherry");
$replacements2 = array(0 => "grape");
$basket = array_replace($base, $replacements, $replacements2);
var_dump($basket);
?>
]]>
&example.outputs;
string(5) "grape"
[1]=>
string(6) "banana"
[2]=>
string(5) "apple"
[3]=>
string(9) "raspberry"
[4]=>
string(6) "cherry"
}
]]>
嵌套数组的处理方式示例
[ 'orange', 'lemon' ], 'pome' => [ 'apple' ] ];
$replacements = [ 'citrus' => [ 'grapefruit' ] ];
$replacements2 = [ 'citrus' => [ 'kumquat', 'citron' ], 'pome' => [ 'loquat' ] ];
$basket = array_replace($base, $replacements, $replacements2);
var_dump($basket);
?>
]]>
&example.outputs;
array(2) {
[0]=>
string(7) "kumquat"
[1]=>
string(6) "citron"
}
["pome"]=>
array(1) {
[0]=>
string(6) "loquat"
}
}
]]>
&reftitle.seealso;
array_replace_recursive
array_merge