Example of deleting array elements in php
This article describes how to delete array elements in php. For more information, see
PHP:
1. Use the unset () method:
The Code is as follows:
<? Php
$ A = array ("red", "green", "blue", "yellow ");
Count ($ a); // get 4
Unset ($ a [1]); // deletes the second element.
Count ($ a); // get 3
Echo $ a [2]; // The array contains only three elements. In this example, we want to get the last element, but we get blue,
Echo $ a [1]; // No value
?>
Disadvantage: After the elements in the array are deleted, the number of elements in the array (obtained by count () has changed, but the array subscript has not been rearranged, you must also use PHP to delete the key before the array element to operate the corresponding value.
2. Use the array_splice () method:
The Code is as follows:
<? Php
$ A = array ("red", "green", "blue", "yellow ");
Count ($ a); // get 4
Array_splice ($ a,); // deletes the second element.
Count ($ a); // get 3
Echo $ a [2]; // obtain yellow
Echo $ a [1]; // get blue
?>
Compared with the previous one, we can see that array_splice () not only deletes the elements, but also rearranges the elements so that no null value exists between the elements in the array!