English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The array_replace_recursive() function uses the passed array to recursively replace the elements of the first array
array_replace_recursive( array $array1 [, array $... ] ) : array
array_replace_recursive() uses the values of the elements in the latter array to replace the array array1 value. If a key exists in both the first and second arrays, its value will be replaced by the value in the second array. If a key exists in the second array but not in the first array, it will be created in the first array. If a key only exists in the first array, it will remain unchanged. If multiple replacement arrays are passed, they will be processed in order, and the later arrays will overwrite the previous values.
array_replace_recursive() is recursive: it will traverse the array and apply the same processing to the internal values of the array.
If the value in the first array is a scalar, it will be replaced by the value in the second array, which may be a scalar or an array. If the values in both the first and second arrays are arrays, the array_replace_recursive() function will recursively replace their respective values.
Returns aarray. If an error occurs, it will return NULL
.
Use the passed array to recursively replace the elements of the first array
<?php $base = array('citrus' => array("orange"), 'berries' => array("blackberry", "raspberry")); $replacements = array('citrus' => array('pineapple'), 'berries' => array('blueberry')); $basket = array_replace_recursive($base, $replacements); print_r($basket); $basket = array_replace($base, $replacements); print_r($basket); ?>Test and see ‹/›
Output result:
Array ( [citrus] => Array ( [0] => pineapple ) [berries] => Array ( [0] => blueberry [1] => raspberry ) ) Array ( [citrus] => Array ( [0] => pineapple ) [berries] => Array ( [0] => blueberry ) )