For example, the following program:
Copy Code code as follows:
<?php
$arr = Array (' Apple ', ' banana ', ' cat ', ' dog ');
Unset ($arr [2]);
Print_r ($arr);
?>
Program Run Result:
Copy Code code as follows:
Array ([0] => Apple [1] => banana [3] => dog)
But the biggest disadvantage of this approach is that it does not reconstruct the array index, which means that the third element of the array is gone.
After checking the data, the original PHP provides this function, but very indirect. This function is Array_splice ().
For ease of use, I encapsulated a function to make it easy for everyone to use:
Copy Code code as follows:
<?php
Function Array_remove (& $arr, $offset)
{
Array_splice ($arr, $offset, 1);
}
$arr = Array (' Apple ', ' banana ', ' cat ', ' dog ');
Array_remove ($arr, 2);
Print_r ($arr);
?>
After testing, we know that 2 of the location of this element was actually deleted and the index was reset.
Program Run Result:
Copy Code code as follows:
Array ([0] => Apple [1] => banana [2] => dog)
PHP Array_splice () function
The Array_splice () function, like the Array_slice () function, selects a series of elements in an array, but does not return them, but instead deletes them and replaces them with other values. If the fourth argument is supplied, those elements that were previously selected 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)
Array: Required. The specified array.
Offset: Required. Numerical. If offset is positive, the offset specified by the value in the input array begins to be removed. If offset is negative, the offset specified from the end of the input array is started to be removed.
Length: Optional. Numerical. If this argument is omitted, all parts of the divisor group from offset to end are moved. If length is specified and positive, then so many elements are removed. If length is specified and negative, all elements in the middle from offset to the end of the array are removed.
Array: The removed element is substituted by the element in this array. If no value is removed, the elements in this array are inserted at the specified location.
If the function does not delete any elements (length=0), the alternate array is inserted from the position of the start parameter.
Example 1:
Copy Code code as follows:
<?php
$a 1=array (0=> "Dog",1=> "Cat",2=> "Horse",3=> "Bird");
$a 2=array (0=> "Tiger",1=> "Lion");
Array_splice ($a 1,0,2, $a 2);
Print_r ($a 1);
?>
Output:array ([0] => Tiger [1] => Lion [2] => horse [3] => Bird)
Example 2:
Copy Code code as follows:
<?php
$a 1=array (0=> "Dog",1=> "Cat",2=> "Horse",3=> "Bird");
$a 2=array (0=> "Tiger",1=> "Lion");
Print_r (Array_splice ($a 1,0,2, $a 2));
?>
Output:array ([0] => Dog [1] => Cat)
Example 3:
Copy Code code as follows:
<?php
The length parameter is set to 0
$a 1=array (0=> "Dog",1=> "Cat");
$a 2=array (0=> "Tiger",1=> "Lion");
Array_splice ($a 1,1,0, $a 2);
Print_r ($a 1);
?>
Output:array ([0] => Dog [1] => Tiger [2] => Lion [3] => Cat)