Home > Article > Backend Development > How to replace old array with new array in php
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.
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('content-type:text/html;charset=utf-8'); $a1=array("red","green"); $a2=array("blue","yellow"); $a3=array("blue"); var_dump(array_replace($a1,$a2)); var_dump(array_replace($a1,$a3)); ?>
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('content-type:text/html;charset=utf-8'); $a1=array("red","green"); $a2=array("blue","yellow"); $a3=array("orange","burgundy"); var_dump(array_replace($a1,$a2,$a3)); ?>
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!