9

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.

8
  • 1
    What is the code, which you used with array_filter()? Commented Dec 28, 2017 at 6:15
  • 2
    array_filter($array,'strlen') Code is worked Commented Dec 28, 2017 at 6:22
  • 1
    @Athi - that would also remove empty strings as well, which is not the same as NULL. You can use a closure as your callback to control what gets filtered. Commented Dec 28, 2017 at 6:25
  • 1
    @AlivetoDie--Anantsingh your coding is working fine. I want to remove empty value Commented Dec 28, 2017 at 6:39
  • 2
    "I used array_filter() to remove the null values" -- your input array does not contain NULL values. It contain empty strings ('') and they are not the same as NULL. NULL means the absence of any value, an empty string is a string of length 0. They are completely different concepts. Commented Dec 28, 2017 at 6:48

7 Answers 7

30

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

Sign up to request clarification or add additional context in comments.

1 Comment

This will also filter out false, in addition to null and ''
16

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); });

4 Comments

Your coding is not working. It returns all values in the array.
@Athi - that's because the array in your question contains no NULL values.
Not your problem :) Even I thought OP said Any builtin function is available in PHP to remove NULL values only. Until an edit
Okay . I understand
4

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
)

Comments

3

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.

Comments

2
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');

Comments

1

As of PHP 7.4:

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

Comments

-2

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);

?>

3 Comments

Whilst code only answers may solve the problem, some explanation would help in understanding this solution and how this may be applied to similar problems.
While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
array_filter without callback will remove any value that can be casted to false by PHP such as '',0, '0', false, null, []. Your claim "Remove null values from array" is bad documentation so I'll add a negative score until proper documentation is provided.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.