Open In App

Remove Falsy Values from an Array in PHP

Last Updated : 15 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

In PHP, falsy values include "false", "null", 0, " " (empty string), '0', and [] (empty array). Removing these values from an array can help ensure that only meaningful and truthy data remains, which can be crucial for accurate data processing and analysis. By filtering out these falsy values, you can clean up your arrays, making them more reliable for further operations and reducing potential errors in your PHP applications.Removing these values helps in retaining only meaningful data in your arrays.

Example:

Input: Array = [0, 1, false, 2, "'', 3, null, "Hello", [], "World"];
Output: Array = [1, 2, 3, "Hello", "World"]

There are two methods to remove Falsy values from the array in PHP:

Using array_filter() function

The array_filter function filters array elements using a callback function and removes all Falsy values by default when no callback is provided.

  • "array_filter(array)" filters out falsy values from the array.
  • The function checks each element and retains only those that are truthy.

Example: This example shows the use of array_filter() function to filter an array.

<?php
$array = [0, 1, false, 2, '', 3, '0', null, [], 4, 'hello', 0.0];

$filteredArray = array_filter($array);

print_r($filteredArray);
?>

Output
Array
(
    [1] => 1
    [3] => 2
    [5] => 3
    [9] => 4
    [10] => hello
)

Using foreach Loop

A foreach loop can be used to iterate through the array and manually check each element, adding only the truthy values to a new array.

  • The foreach loop iterates through each element of the array.
  • Only elements that evaluate to true are added to the filteredArray.

Example: This example shows the use of foreach loop to filter an array.

<?php
$array = [0, 1, false, 2, '', 3, '0', null, [], 4, 'hello', 0.0];
$filteredArray = [];

foreach ($array as $value) {
    if ($value) {
        $filteredArray[] = $value;
    }
}
print_r($filteredArray);
?>

Output
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => hello
)

Using array_diff with an array of falsy values

This approach uses array_diff($arr, $falsyValues) where $arr is the original array and $falsyValues is an array containing values like false, null, 0, '', [], and 0.0. It returns a new array with elements from $arr that do not match any value in $falsyValues, effectively removing falsy values from the array while preserving the original order of elements.

Example: This example shows the use of array_diff with an array of falsy values to filter an array.

<?php
function removeFalsyValues($arr) {
    $falsyValues = [false, null, 0, '', [], 0.0];
    return array_diff($arr, $falsyValues);
}

$array = [1, 0, 'hello', '', null, false, [], 42, 'geek'];

$result = removeFalsyValues($array);
print_r($result);
?>

Output
Array
(
    [0] => 1
    [2] => hello
    [7] => 42
    [8] => geek
)



Next Article

Similar Reads

three90RightbarBannerImg