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 ensure that missing elements are filled in and the array is re-indexed? 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 specific elements in the array
<?php $arr2 = array(1,3, 5,7,8); foreach ($arr2 as $key=>$value) { if ($value === 3) unset($arr2[$key]); } var_dump($arr2); ?>
Supplementary deletion of empty arrays
Example:
<?php $array = ('a' => "abc", 'b' => "bcd",'c' =>"cde",'d' =>"def",'e'=>""); array_filter($array); echo "<pre class="brush:php;toolbar:false">"; 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 will also change.
If the unset() function deletes it, the index value of the array will not change.
unset can delete all variables. array_pop only operates on arrays and only pops the last element of the array. unset can delete any element in the array
I don’t know. Do you know the array_slice function?
$arr = array_slice($arr, 0, 3);
That’s it.
array_slice() The first parameter is the array to be cut, the second parameter is the starting position, and the third parameter is the length.
It is to cut the $arr array and count 3 elements from the 0th element downwards.
array_slice is very flexible in usage and can support negative parameters. You can check the PHP manual for details.
cn.php.net/manual/en/function.range.php