Remove Null, False, and '' - But Not 0 - from a PHP Array

Remove NULL, FALSE, and '' - but not 0 - from a PHP array

array_filter should work fine if you use the identical comparison operator.

here's an example

$values = [NULL, FALSE, '', 0, 1];

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

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

Or if you don't want to define a filtering function, you can also use an anonymous function (closure):

$res = array_filter($values, function($value) {
return ($value !== null && $value !== false && $value !== '');
});

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

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

Remove empty values from PHP array but keep 0

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

Remove empty array elements

As you're dealing with an array of strings, you can simply use array_filter(), which conveniently handles all this for you:

print_r(array_filter($linksArray));

Keep in mind that if no callback is supplied, all entries of array equal to FALSE (see converting to boolean) will be removed. So if you need to preserve elements that are i.e. exact string '0', you will need a custom callback:

// PHP 7.4 and later
print_r(array_filter($linksArray, fn($value) => !is_null($value) && $value !== ''));

// PHP 5.3 and later
print_r(array_filter($linksArray, function($value) { return !is_null($value) && $value !== ''; }));

// PHP < 5.3
print_r(array_filter($linksArray, create_function('$value', 'return $value !== "";')));

Note: If you need to reindex the array after removing the empty elements, use: $linksArray = array_values(array_filter($linksArray));

Cannot remove null elements from nested array

I've an one liner solution to filter out null values from multidimensional array if you use array_map along with array_filter,

    $array = [
['Proposal_id' => 9,
'row' => 1,
'col1' => 2,
'col2' => 2,
'col3' => null,
'col4' => null,
'col5' => null,
'Type' => 'customtbl',
'Invoice_term' => null,
'Qoute' => null,
'Rate_per_hour' => null,
'Total' => null,
],
[
'Proposal_id' => 9,
'row' => 1,
'col1' => 2,
'col2' => 2 ,
'col3' => null,
'col4' => null,
'col5' => null,
'Type' => 'customtbl',
'Invoice_term' => null,
'Qoute' => null,
'Rate_per_hour' => null,
'Total' => null,
]
];
$af = array_filter(array_map('array_filter', $array));
print '<pre>';
print_r($af);
print '</pre>';

Output:

 Array
(
[0] => Array
(
[Proposal_id] => 9
[row] => 1
[col1] => 2
[col2] => 2
[Type] => customtbl
)

[1] => Array
(
[Proposal_id] => 9
[row] => 1
[col1] => 2
[col2] => 2
[Type] => customtbl
)

)

DEMO : https://eval.in/975240

Filter recursive array and only remove NULL value

You could combine array_map and array_filter in an recursive called function. Something like this could work for you.

function filterNotNull($array) {
$array = array_map(function($item) {
return is_array($item) ? filterNotNull($item) : $item;
}, $array);
return array_filter($array, function($item) {
return $item !== "" && $item !== null && (!is_array($item) || count($item) > 0);
});
}

array_filter skip null or 0 value

array_filter will remove all elements having null, empty and 0 values.
To handle value 0 in your case, callback function can be used where you can explicitly specify which values to allow and which to ignore.

I have created a custom callback function that will accept 0 but filter null, empty and false values.

<?php
$url = "log/getLog/0/categorias/5/";

$urlParts = array_filter(explode('/', $url), function($v){
return $v !== false && !is_null($v) && ($v != '' || $v == '0');
});

print_r($urlParts);

$url = array(
0 => 'foo',
1 => false,
2 => -1,
3 => null,
4 => '',
5 => 0
);

$urlParts = array_filter($url, function($v){
return $v !== false && !is_null($v) && ($v != '' || $v == '0');
});

print_r($urlParts);
?>

Output

Array
(
[0] => log
[1] => getLog
[2] => 0
[3] => categorias
[4] => 5
)
Array
(
[0] => foo
[2] => -1
[5] => 0
)

Working Demo

php remove empty elements, but dont remove '0'

$outaa = array_remove_empty($outaa);

function array_remove_empty($arr){
$narr = array();
while(list($key, $val) = each($arr)){
if (is_array($val)){
$val = array_remove_empty($val);
// does the result array contain anything?
if (count($val)!=0){
// yes :-)
$narr[$key] = $val;
}
}
else {
if (trim($val) != ""){
$narr[$key] = $val;
}
}
}
unset($arr);
return $narr;
}

array_filter skip null or 0 value

array_filter will remove all elements having null, empty and 0 values.
To handle value 0 in your case, callback function can be used where you can explicitly specify which values to allow and which to ignore.

I have created a custom callback function that will accept 0 but filter null, empty and false values.

<?php
$url = "log/getLog/0/categorias/5/";

$urlParts = array_filter(explode('/', $url), function($v){
return $v !== false && !is_null($v) && ($v != '' || $v == '0');
});

print_r($urlParts);

$url = array(
0 => 'foo',
1 => false,
2 => -1,
3 => null,
4 => '',
5 => 0
);

$urlParts = array_filter($url, function($v){
return $v !== false && !is_null($v) && ($v != '' || $v == '0');
});

print_r($urlParts);
?>

Output

Array
(
[0] => log
[1] => getLog
[2] => 0
[3] => categorias
[4] => 5
)
Array
(
[0] => foo
[2] => -1
[5] => 0
)

Working Demo

Removing null or blank values of array

You can loop through the array, look for empty variables and use unset to remove them.


This code will loop through and check if the length of the first value in each array is at least one character long and unset it if its not.

<?php
foreach($data as $key => $value) {
if(!isset($value[0][0]))
unset($data[$key]);
}

This code will loop through the array in a similar way, except to check every value of every array to determine if its parrent array should be kept or left to be unset.

<?php
foreach($data as $key => $values) {
foreach($values as $value) {
if(isset($value[0]))
continue 2;
}
unset($data[$key]);
}


Related Topics



Leave a reply



Submit