How to remove empty elements from array in PHP? (code example)

青灯夜游
Release: 2023-04-04 16:48:02
Original
6247 people have browsed it

In a given array containing elements, how to delete empty elements in the array, such as empty strings, NULL elements, etc.? The following article will show you how to delete empty elements in an array. I hope it will be helpful to you.

How to remove empty elements from array in PHP? (code example)

Method 1: Use empty() function and unset() function

empty() function Used to check if an element is empty.

The unset() function is used to unset the specified variable, and its behavior depends on different things.

Example:

<?php 
header("content-type:text/html;charset=utf-8");
// 声明和初始化数组
$array = array("php", 11, &#39;&#39;, null, 12, "javascript", 2018, false, "mysql"); 
echo ("<br>");  
// 显示原数组元素   
echo ("原数组元素:<br>");  
foreach($array as $key => $value)          
    echo ("&#39;".$array[$key] . "&#39;   ");   
echo ("<br><br><br>");          
 
// 循环查找空元素并取消设置空元素
foreach($array as $key => $value)          
    if(empty($value)) 
        unset($array[$key]); 
          
// 显示新数组元素   
echo ("新数组元素:<br>");        
foreach($array as $key => $value)          
    echo ("&#39;".$array[$key] . "&#39;   "); 
?>
Copy after login

Output:

How to remove empty elements from array in PHP? (code example)

##Method 2: Use array_filter() function

The array_filter() function, also known as the callback function, is used to filter the elements of an array using a user-defined function. It iterates over each value in the array, passing them to a user-defined function or callback function.

When the array_filter() function is used to declare a callback function, it will delete false values, but if the callback function is not specified, all values ​​in the array that are equal to FALSE, such as empty strings or NULL values, will be deleted .

Example:

<?php 
header("content-type:text/html;charset=utf-8");
// 声明和初始化数组
$array = array("php", 11, &#39;&#39;, null, 12, "javascript", 2018, false, "mysql"); 
echo ("<br>");  
// 显示原数组元素   
echo ("原数组元素:<br>");  
foreach($array as $key => $value)          
    echo ("&#39;".$array[$key] . "&#39;   ");   
echo ("<br>");          
 
// 使用array_filter()函数从数组中移除空元素
$filtered_array = array_filter($array); 
          
// 显示新数组元素   
echo ("新数组元素:");        
//foreach($array as $key => $value)          
//  echo ("&#39;".$array[$key] . "&#39;   "); 
var_dump($filtered_array); 
?>
Copy after login
Output:


How to remove empty elements from array in PHP? (code example)

The above is the entire content of this article, I hope it can help everyone learn Helps. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to remove empty elements from array in PHP? (code example). 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!