How to delete empty array recursively in php

藏色散人
Release: 2023-03-09 09:26:01
Original
2272 people have browsed it

php method to recursively delete empty arrays: first create a PHP sample file; then delete all empty value elements in the array through the "function array_remove_empty($arr){...}" method.

How to delete empty array recursively in php

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

php recursively calls to delete array null elements The method

The example of this article describes the method of php recursively calling to delete the null value element of the array. Share it with everyone for your reference. The details are as follows:

This function can delete all null elements in the array, including empty strings, empty arrays, etc.

function array_remove_empty($arr){ $narr = array(); while(list($key, $val) = each($arr)){ if (is_array($val)){ $val = array_remove_empty($val); // does the result array contain anything? if (count($val)!=0){ // yes :-) $narr[$key] = $val; } } else { if (trim($val) != ""){ $narr[$key] = $val; } } } unset($arr); return $narr; }
Copy after login

Demonstration example:

The code is as follows:

array_remove_empty(array(1,2,3,'',array(),4)) => returns array(1,2,3,4)
Copy after login

[Recommended learning:PHP video tutorial]

The above is the detailed content of How to delete empty array recursively in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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
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!