English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
array_replace() function fills an array with given values
array_replace ( array $array1 [, array $... ] ) : array
array_replace() function uses the values of the elements with the same key from the subsequent array to replace the array1 values of the array. If a key exists in both the first and second arrays, its value will be replaced by the value from the second array. If a key exists in the second array but not in the first, it will create this element in the first array. If a key exists only in the first array, it will remain unchanged. If multiple replacement arrays are passed, they will be processed in order, with the later arrays overwriting the values of the previous ones.
array_replace() is non-recursive: it replaces the values of the first array regardless of what is in the second array.
Returns anarray. If an error occurs, it will return NULL
.
Fill an array with specified values
<?php $base = array("orange", "banana", "apple", "raspberry"); $replacements = array(0 => "pineapple", 4 => "cherry"); $replacements2 = array(0 => "grape"); $basket = array_replace($base, $replacements, $replacements2); print_r($basket); ?>Test and see ‹/›
Output result:
Array ( [0] => grape [1] => banana [2] => apple [3] => raspberry [4] => cherry )