Home > Article > Backend Development > How to clear array elements in php
The way PHP clears array elements is to use the unset function, such as [unset($array[1]);]. It should be noted that if we use the unset function to clear the array elements, it will not change other keys of the array.
The operating environment of this article: windows10 system, php 7.3, thinkpad t480 computer.
If we want to delete an element in the array, then we can use the unset() or array_splice() method. Here we introduce the unset method.
Note: If we use the unset() method, it will not change other keys (keys). If we want to reorder other keys (keys), we can use the array_values() method.
Implementation code:
<?php $array = array(0 => "a", 1 => "b", 2 => "c"); unset($array[1]); //↑ 你要删除的数组元素值的键print_r($array); ?>
Output result:
Array ( [0] => a [2] => c )
Related video tutorial sharing: php video tutorial
The above is the detailed content of How to clear array elements in php. For more information, please follow other related articles on the PHP Chinese website!