It is very simple to delete array elements in php. However, sometimes we need to sort indexes to delete arrays. we will use related functions. here we will introduce the use of unset, the difference between the elements in the array deleted by array_splice. if you want to delete an element in an array, you can use unset directly, but the index of the array will not be rearranged:
<?php $arr = array('a','b','c','d');unset($arr[1]);print_r($arr);?>
The result is:
Array ([0] => a [2] => c [3] => d)
So how can we fill in the missing elements and re-index the array? The answer is array_splice ():
<?php $arr = array('a','b','c','d'); array_splice($arr,1,1); print_r($arr); ?>
The result is:
Array ([0] => a [1] => c [2] => d)
Delete special elements in an array
<?php$arr2 = array(1,3, 5,7,8);foreach ($arr2 as $key=>$value){ if ($value === 3) unset($arr2[$key]);}var_dump($arr2);?>
Add or delete an empty array
Instance:
<? Php $ array = ('a' => "abc", 'B' => "bcd", 'C' => "cde ", 'D' => "def", 'E' => ""); array_filter ($ array); echo""; print_r($array);?>
Result:
Array (
[A] => abc
[B] => bcd
[C] => cde
[D] => def
)
Summary
If the array_splice () function is deleted, the index value of the array also changes.
If the unset () function is deleted, the index value of the array remains unchanged.