If you want to delete an element in an array, you can use the unset directly, but the index of the array does not reflow:
| 12345 |
<?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 the missing elements be filled and the array will be re-indexed? The answer is Array_splice ():
| 12345 |
<?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 a specific element in an array
| 123456789 |
<?php$arr2= array(1,3, 5,7,8);foreach ($arr2 as $key=>$value){ if ($value=== 3) unset($arr2[$key]);}var_dump($arr2);?> |
Supplemental Delete Empty Array
Instance:
| 123456 |
<?php $array= (‘a‘ => "abc", ‘b‘ => "bcd",‘c‘ =>"cde",‘d‘ =>"def",‘e‘=>""); array_filter($array); echo"<pre>"; print_r($array);?> |
Results:
Array (
[A] = ABC
[B] = BCD
[C] = CDE
[d] = def
)
Summarize
If the Array_splice () function is deleted, the index value of the array also changes.
If the unset () function is removed, the index value of the array does not change.
Unset,array_splice in PHP removes the difference between elements in an array