Home >Backend Development >PHP Problem >How to replace old array with new array in php

How to replace old array with new array in php

青灯夜游
青灯夜游Original
2022-05-26 19:08:451807browse

In PHP, you can use the array_replace() function to replace the old array with a new array. The syntax is "array_replace(old array, new array)"; this function can be replaced with the array specified by the second parameter. For the array specified by the first parameter, the number of elements in the new array must be greater than or equal to the number of elements in the old array.

How to replace old array with new array in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In php, you can use the array_replace() function Replace the old array with the new array.

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

Syntax:

array_replace(旧数组,新数组)

Description: The number of elements in the new array must be greater than or equal to the number of elements in the old array, otherwise only part of the element values ​​can be replaced.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$a1=array("red","green");
$a2=array("blue","yellow");
$a3=array("blue");
var_dump(array_replace($a1,$a2));
var_dump(array_replace($a1,$a3));
?>

How to replace old array with new array in php

Description:

array_replace() function can have multiple arrays used for replacement.

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.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$a1=array("red","green");
$a2=array("blue","yellow");
$a3=array("orange","burgundy");
var_dump(array_replace($a1,$a2,$a3));
?>

How to replace old array with new array in php

It can be seen that the last array ($a3) will overwrite the previous arrays ($a1 and $a2).

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to replace old array with new array in php. 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