Mukhila Asokan
Mukhila Asokan

Reputation: 653

Remove empty values from PHP array but keep 0

I have an array

array (size=7)
0 => string '2' 
1 => string '' 
2 => string '' 
3 => string '0' 
4 => string '' 
5 => string '' 
6 => string '2.5' 

I used:-

array_filter()

to remove the null values and it returns

Array ( [0] => 2 [6] => 2 )

array_filter() removes null value and also '0' value.

Any builtin function is available in PHP to remove NULL values only.

Upvotes: 9

Views: 31854

Answers (7)

Saroar Hossain Limon
Saroar Hossain Limon

Reputation: 75

In case your array could contain line feeds and you want to filter them out as well, you may want to trim values first:

$arrayItems = ["\r", "\n", "\r\n", "", " ", "0", "a"];
$newArray = array_values(array_filter(array_map('trim', $arrayItems), 'strlen'));
// Output(newArray): ["0", "a"]

//Another great snippet to remove empty items from array
$newArray = array_filter($arrayItems , function ($item) {
    return !preg_match('/^\\s*$/', $item);
    // filter empty lines
});
// Output(newArray): ["0", "a"]

If you face any issue with implementing the code just comment here below.

Upvotes: 3

Matt Janssen
Matt Janssen

Reputation: 1663

As of PHP 7.4:

$withoutNulls = array_filter($withNulls, static fn($e) => $e !== null);

Upvotes: 0

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

Assumption: I think you want to remove NULL as well as empty-strings/values '' from your array. (What i understand from your desired output)

You have to use array_filter() with strlen()

array_filter($array,'strlen');

Output:-

https://eval.in/926585

https://eval.in/926595

https://eval.in/926602

Refrence:-

PHP: array_filter - Manual

PHP: strlen - Manual

Upvotes: 30

Festus Ngor
Festus Ngor

Reputation: 311

You can remove empty value and then re-index the array elements by using array_values() and array_filter() function together as such:

$arr = array("PHP", "HTML", "CSS", "", "JavaScript", null, 0);
print_r(array_values(array_filter($arr)));

Output:

Array
(
    [0] => PHP
    [1] => HTML
    [2] => CSS
    [3] => JavaScript
)

Upvotes: 4

Mazhar Hussain
Mazhar Hussain

Reputation: 125

Remove null values from array

$array = array("apple", "", 2, null, -5, "orange", 10, false, "");

var_dump($array);

echo "<br>";



// Filtering the array

$result = array_filter($array);                 

var_dump($result);

?>

Upvotes: -2

You Old Fool
You Old Fool

Reputation: 22941

Both 0 and '' are considered falsey in php, and so would be removed by array filter without a specific callback. The suggestion above to use 'strlen' is also wrong as it also remove an empty string which is not the same as NULL. To remove only NULL values as you asked, you can use a closure as the callback for array_filter() like this:

array_filter($array, function($v) { return !is_null($v); });

Upvotes: 16

TarangP
TarangP

Reputation: 2738

function myFilter($var){
  return ($var !== NULL && $var !== FALSE && $var !== '');
}

$res = array_filter($yourArray, 'myFilter');

If you just need the numeric values you can use is_numeric as your callback

Demo

$res = array_filter($values, 'is_numeric');

Upvotes: 2

Related Questions