You are here: Home / Topics / How to remove null or empty values from an array in PHP

How to remove null or empty values from an array in PHP

Filed under: PHP on 2023-07-02 21:17:47

You can use the PHP array_filter() function to remove or filter empty values from an array. This function usually filters the values of an array using a callback function. However, if no callback is specified, all values in the array equal to FALSE will be removed, such as an empty string or a NULL value.
 

How to remove null or empty values from an array in PHP
 

<?php
 $arr = array("", "lorem", 0, null, "ipsum", false);
 // Filter the array
 $new_arr = array_filter($arr);                 
 print_r($new_arr);
?>
 

Output:

Array ( 
[1] => lorem 
[4] => 1 
[5] => ipsum 
)

About Author:
Y
Yatendra Sir     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.