10

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.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Mukhila Asokan
  • 641
  • 3
  • 11
  • 29
  • 1
    What is the code, which you used with `array_filter()`? – Geshode Dec 28 '17 at 06:15
  • ` '2', 1 => '', 2 => '', 3 => '0', 4 => '', 5 => '', 6 => '2' ); print_r(array_filter($entry)); ?>` This is my code I used array_filter() and it removes 0 value – Mukhila Asokan Dec 28 '17 at 06:17
  • https://stackoverflow.com/questions/3654295/remove-empty-array-elements see here – sunil Dec 28 '17 at 06:17
  • 2
    `array_filter($array,'strlen')` Code is worked – Mukhila Asokan Dec 28 '17 at 06: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. – But those new buttons though.. Dec 28 '17 at 06:25
  • @Athi `NULL != ''`, so i think you want to remove null as well as empty strings too from your array.Isn't it? – Alive to die - Anant Dec 28 '17 at 06:38
  • 1
    @AlivetoDie--Anantsingh your coding is working fine. I want to remove empty value – Mukhila Asokan Dec 28 '17 at 06: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`](http://php.net/manual/en/language.types.null.php) means the absence of any value, an empty string is a string of length `0`. They are completely different concepts. – axiac Dec 28 '17 at 06:48

7 Answers7

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

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
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

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
)
Festus Ngor
  • 301
  • 2
  • 6
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');
TarangP
  • 2,711
  • 5
  • 20
  • 41
2

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.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0

As of PHP 7.4:

$withoutNulls = array_filter($withNulls, static fn($e) => $e !== null);
Matt Janssen
  • 1,505
  • 13
  • 14
-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);

?>
Mazhar Hussain
  • 125
  • 1
  • 11
  • 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. – Nigel Ren Dec 28 '17 at 11:32
  • While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) 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. – yivi Dec 28 '17 at 11:44
  • 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. – Alessandro Benoit Oct 23 '19 at 09:53