PHP code to delete specific elements from an array

  • 2020-05-19 04:18:13
  • OfStack

For example, the following program:
 
<?php 

$arr = array('apple','banana','cat','dog'); 

unset($arr[2]); 
print_r($arr); 

?> 

Program running results:
 Array ( [0] => apple [1] => banana [3] => dog ) 

However, the biggest disadvantage of this method is that there is no reconstruction of the array index, that is, the third element of the array is gone.
After a search, it turned out that PHP provided this functionality, albeit indirectly. This function is array_splice().
For convenience, I encapsulated it into a function for everyone to use:
 
<?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, it can be known that the position of 2 is actually deleted and re-indexed.
Program running results:
 
Array ( [0] => apple [1] => banana [2] => dog ) 

PHP array_splice () function
The array_splice() function is similar to the array_slice() function in that it selects the series 1 elements in the array, but instead of returning them, removes them and replaces them with other values. If the fourth argument is provided, the previously selected elements are replaced by the array specified by the fourth argument.
The resulting array will be returned.
Grammar: array_splice (array, offset length, array)
array: required. Specify an array.
offset: required. Numerical values. If offset is positive, it is removed from the offset specified by the value in the input array. If offset is negative, it is removed from the offset specified at the reciprocal of the value at the end of the input array.
length: optional. Numerical values. If you omit this parameter, you remove all parts of the array from offset to the end. If length is specified and positive, you remove so many elements. If length is specified and is negative, all elements between offset and the reciprocal length at the end of the array are removed.
array: the removed element is replaced by the element in the array. If no value is removed, the elements in this array are inserted into the specified location.
If the function does not remove any elements (length=0), the substitution array is inserted from the location of the start parameter.
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:
 
<?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:
 
<?php 
// length  Parameter set to  0 
$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 ) 

Related articles: