Home > Article > Backend Development > splitViewController php array function sequence array_splice - insert elements at any position in the array
array_splice definition and usage
array_splice() function is similar to array_slice() function, selecting a series of elements in the array, but instead of returning them, it 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 with elements from this array. If no values are removed, the element in this array is inserted at the specified position.
Tips and Notes
Tip: If the function does not remove any elements (length=0), the replacement array will be inserted from the position of the start parameter. (See Example 3)
Note: Keys in the substitution array are not preserved.
Example 1
Copy the code The code is as follows:
$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);
?>
Copy code The code is as follows:
$a1=array(0=>"Dog",1=>"Cat",2 =>"Horse",3=>"Bird");
$a2=array(0=>"Tiger",1=>"Lion");
print_r(array_splice($a1,0,2 . The code is as follows:
?> Output: Array ( [0] => Dog [1] = > Tiger [2] => Lion [3] => Cat )
The above introduces array_splice of the splitViewController PHP array function sequence - inserting elements at any position in the array, including splitViewController content. I hope it will be helpful to friends who are interested in PHP tutorials.