Home > Backend Development > PHP Problem > What are the php array replacement functions?

What are the php array replacement functions?

青灯夜游
Release: 2023-03-10 09:50:01
Original
2928 people have browsed it

php array replacement functions include: 1. array_replace() function, which uses the value of the following array to replace the value of the first array; 2. array_replace_recursive() function, which recursively uses the value of the following array to replace the first one. The value of the array; 3. array_splice() function.

What are the php array replacement functions?

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

1.array_replace() function

array_replace() function replaces the value of the first array with the value of the subsequent array. The syntax is as follows:

array_replace(array1,array2,array3...)
Copy after login
  • 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 second array Replace the values ​​in array2.

  • If a key only exists in the first array array1, it will remain unchanged.

  • 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.

  • 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.

Example 1: If a key exists in array1 and also exists in array2, the value of the first array will be replaced by the value of the second array

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

Output:

Array ( [a] => orange [b] => green [0] => burgundy )
Copy after login

Example 2: If a key only exists in the second array:

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

Output:

Array ( [a] => orange [0] => green [b] => burgundy )
Copy after login

2, array_replace_recursive() function

array_replace_recursive() function recursively replaces the value of the first array with the value of the subsequent array. The syntax is as follows:

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

Example: Multiple arrays

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

Output:

Array ( [a] => Array ( [0] => orange ) [b] => Array ( [0] => burgundy [1] => blue ) )
Copy after login

Explanation: The difference between array_replace() and array_replace_recursive():

<?php
$a1=array("a"=>array("red"),"b"=>array("green","blue"),);
$a2=array("a"=>array("yellow"),"b"=>array("black"));

$result=array_replace_recursive($a1,$a2);
print_r($result);

$result=array_replace($a1,$a2);
print_r($result);
?>
Copy after login

Output:

Array ( [a] => Array ( [0] => yellow ) [b] => Array ( [0] => black [1] => blue ) ) 
Array ( [a] => Array ( [0] => yellow ) [b] => Array ( [0] => black ) )
Copy after login

3. array_splice() function

array_splice() function removes the selected element from the array and replaces it with the new element replace it. This function will also return the array containing the removed elements. If the function does not remove any elements (length=0), the replaced array will be inserted from the position of the start parameter.

Syntax

array_splice(array,start,length,array)
Copy after login
ParametersDescription
array Required. Specifies an array.
start

Required. numerical value. Specifies the starting position of deleted elements.

  • 0 = first element.
  • If the value is set to a positive number, removal starts from the offset specified by the value in the array.
  • If the value is set to a negative number, removal starts from the offset specified by the value from the end of the array.
  • -2 means start from the second to last element of the array.
length

Optional. numerical value. Specifies the number of elements to be removed, which is also the length of the returned array.

  • If the value is set to a positive number, remove that number of elements.
  • If the value is set to a negative number, all elements from start to the reciprocal length of the end of the array are removed.
  • If this value is not set, all elements starting from the position set by the start parameter to the end of the array are removed.
array

Optional. Specifies the array with the elements to be inserted into the original array.

If there is only one element, it can be set to a string and does not need to be set to an array.

Example: Set the length parameter to 0:

<?php
$a1=array("0"=>"red","1"=>"green");
$a2=array("0"=>"purple","1"=>"orange");
array_splice($a1,1,0,$a2);
print_r($a1);
?>
Copy after login

Output:

Array ( [0] => red [1] => purple [2] => orange [3] => green )
Copy after login

Recommended learning: " PHP video tutorial

The above is the detailed content of What are the php array replacement functions?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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