How to remove empty values from an array in PHP

  1701 views   2 years ago PHP

Use the PHP array_filter() function

You can use the PHP array_filter() function to remove or filter empty or false values from an array. This function typically filters the values of an array using a callback function, however if no callback function is specified, all the values of the array which are equal to FALSE will be removed, such as an empty string or a NULL value. Let's take a look at an example:

<?php
$array = array("apple", "", 2, null, -5, "orange", 10, false, "");
var_dump($array);
echo "<br>";
 
// Filtering the array
$result = array_filter($array);                 
var_dump($result);
?>

 

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]