Home>Article>Backend Development> How to delete all elements of array in php
The way PHP deletes all elements of an array is to use the unset function to directly delete the array variables and completely release the resources, so that the array is equivalent to being cleared. We can also redefine the array as an empty array, which also clears the array elements.
The operating environment of this article: windows10 system, php 7.3, thinkpad t480 computer.
If we want to delete all elements in an array, then we can do it through the following three ideas.
For example, now we have an array like this, so how do we clear the array?
$array1 = array("item1", "item2", "item3");
Clear the value of the array and keep the key
foreach($array1 as $key=>$value){ unset($array1[$value]); }
The result of count($array1) is 3
Redefine the array as an empty array
$array1 = array();
Delete the array variable and completely release the resources
unset($array1);
Related video tutorial sharing:php video tutorial
The above is the detailed content of How to delete all elements of array in php. For more information, please follow other related articles on the PHP Chinese website!