How to delete elements (unset, array_splice) in the PHP array )?
If you want to delete an element from 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)
Articles you may be interested in
- PHP removes the null element from the array (array_filter)
- Php searches for the existence of a value in the array (in_array (), array_search (), array_key_exists ())
- How does php Delete the first and last elements of an array?
- JavaScript array operation function Summary (push, pop, join, shift, unshift, slice, splice, concat)
- PHP merges arrays + and array_merge
- Php clears the null element in the array.
- PHP Array Function array_walk () Notes
- Php retrieves the last element of the array