Home  >  Article  >  Backend Development  >  PHP inserts elements at any position in the array. Detailed explanation of the usage of array_splice() function

PHP inserts elements at any position in the array. Detailed explanation of the usage of array_splice() function

伊谢尔伦
伊谢尔伦Original
2017-06-24 10:16:133989browse

array_spliceDefinition and usage

array_splice() Function and array_slice# The ##() function is similar, selecting a series of elements in an array , but instead of returning them, deletes them and replaces them with other values. If the fourth parameter is provided, those previously selected elements will be replaced by the array specified by the fourth parameter.
The last generated array will be returned.
Syntax
array_splice(array,offset,length,array) Parameter Description
array Required. Specifies an array.
offset required. numerical value. If offset is positive, removal begins at the offset specified by this value in the input array. If offset is negative, removal begins at the offset specified by this value from the end of the input array.
length Optional. numerical value. If this parameter is omitted, all parts of the array from offset to the end are removed. If length is specified and is positive, this many elements are removed. If length is specified and is negative, all elements from offset to length counting down from the end of the array are removed.
array The removed elements are replaced by elements in this array. If no values ​​are removed, the element in this array is inserted at the specified position.
Tips and
Comments Tip: If the function does not remove any elements (length=0), the substitution array will be inserted from the position of the start parameter. (See Example 3)
Comment: Keys in the substitution array are not retained.
Example 1

<?php 
$a1=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird"); 
$a2=array(0=>"Tiger",1=>"Lion"); 
array_splice($a1,0,2,$a2); 
print_r($a1); 
?>

Output:


Array ( [0] => Tiger [1] => Lion [2] => Horse [3] => Bird )

Example 2 is the same as Example 1, but outputs the returned array:

<?php 
$a1=array(0=>"Dog",1=>"Cat",2=>"Horse",3=>"Bird"); 
$a2=array(0=>"Tiger",1=>"Lion"); 
print_r(array_splice($a1,0,2,$a2)); 
?>

Output:


Array ( [0] => Dog [1] => Cat )

Example 3 The length parameter is set to 0:

<?php 
$a1=array(0=>"Dog",1=>"Cat"); 
$a2=array(0=>"Tiger",1=>"Lion"); 
array_splice($a1,1,0,$a2); 
print_r($a1); 
?>

Output:


Array ( [0] => Dog [1] => Tiger [2] => Lion [3] => Cat )

The above is the detailed content of PHP inserts elements at any position in the array. Detailed explanation of the usage of array_splice() function. 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