Delete an element from an array. I use unset directly, regardless of what happens after unset. However, what I saw today surprised me, not as I thought. I use examples to illustrate an example:
The Code is as follows:
$ Arr = array ('A', 'B', 'C', 'D ');
Unset ($ arr [1]);
Print_r ($ arr );
?>
I previously imagined that after unset, the array $ arr should compress the array to fill in the missing element location, but after print_r ($ arr), the result is not that, the final result is Array ([0] => a [2] => c [3] => d );
If so, let's look at the form of a number array.
The Code is as follows:
$ Arr = range (5, 10, 4 );
Print_r ($ arr ); // Array ([0] => 5 [1] => 6 [2] => 7 [3] => 8 [4] => 9 [5] => 10)
Unset ($ arr [1]); // Array ([0] => 5 [2] => 7 [3] => 8 [4] => 9 [5] => 10)
Print_r ($ arr );
?>
The output format is also the position where the array fills the missing element. So how can we fill in the missing elements and re-index the array? The answer is array_splice ():
The Code is as follows:
$ Arr = array ('A', 'B', 'C', 'D ');
Array_splice ($ arr, 1, 1 );
Print_r ($ arr); // Array ([0] => a [1] => c [2] => d)
?>