Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Null values in PHP array

Tags:

php

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.

like image 950
Mukhila Asokan Avatar asked Dec 28 '17 06:12

Mukhila Asokan


1 Answers

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

like image 134
Anant Kumar Singh Avatar answered Oct 14 '22 08:10

Anant Kumar Singh