Home>Article>Backend Development> How to delete all elements of array in php

How to delete all elements of array in php

王林
王林 Original
2021-07-15 16:15:39 3515browse

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.

How to delete all elements of array in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn