Home > Backend Development > PHP Tutorial > php array replacement function array_replace()

php array replacement function array_replace()

PHP中文网
Release: 2023-03-16 21:12:02
Original
1979 people have browsed it

<?php
$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_replace($a1,$a2));
?>
Copy after login

Definition and usage

array_replace() function replaces the value of the first array with the value of the following array.

Tip: You can pass an array, or multiple arrays, to the function.

If a key exists in the first array array1 and also exists in the second array array2, the value in the first array array1 will be replaced by the value in the second array array2. If a key only exists in the first array array1, it will remain unchanged. (See Example 1 below for details)

If a key exists in the second array array2, but does not exist in the first array array1, the element will be created in the first array array1. (See Example 2 below for details)

If multiple replacement arrays are passed, they will be processed in order, and the values ​​of the subsequent arrays will overwrite the values ​​of the previous arrays. (See Example 3 below for details)

Tip: Please use array_replace_recursive() to recursively replace the value of the first array with the value of the subsequent array.

Syntax

array_replace(array1,array2,array3...)
Copy after login

Parameters   Description

array1 Required. Specify an array.

array2 Optional. Specifies an array to replace the value of array1 .

array3,... Optional. Specify multiple arrays to replace the values ​​of array1 and array2, ... . The values ​​of the following array will overwrite the values ​​of the previous array.

Technical details

Return value:

Returns the replaced array, or NULL if an error occurs.

If a key exists in the first array array1 and also exists in the second array array2, the value in the first array array1 will be replaced by the value in the second array array2. If a key only exists in the first array array1, it will remain unchanged.

<?php
$a1=array("a"=>"red","b"=>"green");
$a2=array("a"=>"orange","burgundy");
print_r(array_replace($a1,$a2));
?>
Copy after login

If a key exists in the second array array2, but does not exist in the first array array1, the element will be created in the first array array1.

<?php
$a1=array("a"=>"red","green");
$a2=array("a"=>"orange","b"=>"burgundy");
print_r(array_replace($a1,$a2));
?>
Copy after login

Use three arrays - the last array ($a3) will overwrite the previous arrays ($a1 and $a2):

<?php
$a1=array("red","green");
$a2=array("blue","yellow");
$a3=array("orange","burgundy");
print_r(array_replace($a1,$a2,$a3));
?>
Copy after login

Use numeric keys - if a key exists in the second Array array2, but does not exist in the first array array1, then this element will be created in the first array array1:

<?php
$a1=array("red","green","blue","yellow");
$a2=array(0=>"orange",3=>"burgundy");
print_r(array_replace($a1,$a2));
?>
Copy after login


The above is the detailed content of php array replacement function array_replace(). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template