array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'uk', ), 'this' => array ( 0 => 'function.array-replace.php', 1 => 'array_replace', 2 => 'Replaces elements from passed arrays into the first array', ), 'up' => array ( 0 => 'ref.array.php', 1 => 'Функції для роботи з масивами', ), 'prev' => array ( 0 => 'function.array-reduce.php', 1 => 'array_reduce', ), 'next' => array ( 0 => 'function.array-replace-recursive.php', 1 => 'array_replace_recursive', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/array/functions/array-replace.xml', ), 'history' => array ( ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); contributors($setup); ?>
(PHP 5 >= 5.3.0, PHP 7, PHP 8)
array_replace — Replaces elements from passed arrays into the first array
array_replace() creates a new array and assigns items into it for each key in each of the provided arrays. If a key appears in multiple input arrays, the value from the right-most input array will be used.
array_replace() does not process elements items recursively, it replaces the entire value for each key when it does a replacement.
arrayThe array in which elements are replaced.
replacementsArrays from which elements will be extracted. Values from later arrays overwrite the previous values.
Returns an array.
Приклад #1 array_replace() example
<?php
$base = array("orange", "banana", "apple", "raspberry");
$replacements = array(0 => "pineapple", 4 => "cherry");
$replacements2 = array(0 => "grape");
$basket = array_replace($base, $replacements, $replacements2);
var_dump($basket);
?>Поданий вище приклад виведе:
array(5) {
[0]=>
string(5) "grape"
[1]=>
string(6) "banana"
[2]=>
string(5) "apple"
[3]=>
string(9) "raspberry"
[4]=>
string(6) "cherry"
}
Приклад #2 Example of how nested arrays are handled
<?php
$base = [ 'citrus' => [ 'orange', 'lemon' ], 'pome' => [ 'apple' ] ];
$replacements = [ 'citrus' => [ 'grapefruit' ] ];
$replacements2 = [ 'citrus' => [ 'kumquat', 'citron' ], 'pome' => [ 'loquat' ] ];
$basket = array_replace($base, $replacements, $replacements2);
var_dump($basket);
?>Поданий вище приклад виведе:
array(2) {
["citrus"]=>
array(2) {
[0]=>
string(7) "kumquat"
[1]=>
string(6) "citron"
}
["pome"]=>
array(1) {
[0]=>
string(6) "loquat"
}
}