Example of deleting array elements in php. PHP: 1. use the unset () method: Copy the code as follows :? Php $ aarray (red, green, blue, yellow); count ($ a); get 4 unset ($ a [1]); delete the PHP-specific method for deleting array elements:
1. use the unset () method:
The code is as follows:
$ 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:
$ 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!
Example 1. use the unset () method: the code is as follows :? Php $ a = array ("red", "green", "blue", "yellow"); count ($ ); // Get 4 unset ($ a [1]); // delete...