How to delete all elements of array in php

王林
Release: 2023-03-11 15:44:01
Original
3526 people have browsed it

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");
Copy after login

Clear the value of the array and keep the key

foreach($array1 as $key=>$value){            
unset($array1[$value]);        }
Copy after login

The result of count($array1) is 3

Redefine the array as an empty array

$array1 = array();
Copy after login

Delete the array variable and completely release the resources

unset($array1);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!