Home  >  Article  >  Backend Development  >  How to replace the content of php array

How to replace the content of php array

PHPz
PHPzOriginal
2020-09-25 14:22:523442browse

In PHP, the array content can be replaced through the "array_replace" function. The syntax of this function is "array_replace (array $array1 [, array $.. ]) : array", where the parameter "array1" represents Replace the array.

How to replace the content of php array

Recommended: "PHP Video Tutorial"

array_replace — Replace the elements of the first array with the passed array

Description

array_replace ( array $array1 [, array $... ] ) : array

array_replace() function replaces the value of array1 array with the value of the same key of the subsequent array element. If a key exists in the first array and also exists in the second array, 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, the element 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 subsequent arrays will overwrite previous values.

array_replace() is non-recursive: it replaces the values ​​of the first array regardless of the type in the second array.

Parameters

array1

Replace the value of the array.

...

The array containing the elements to be extracted. The values ​​in the subsequent array will overwrite the previous values.

Return value

Returns an array. If an error occurs, NULL will be returned.

Example

Example #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);
print_r($basket);
?>

The above routine will output:

Array
(
    [0] => grape
    [1] => banana
    [2] => apple
    [3] => raspberry
    [4] => cherry
)

The above is the details of how to replace the content of the php array Content, please pay attention to other related articles on the php Chinese website for more information!

The above is the detailed content of How to replace the content of php array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn