I want to remove null values from this array.
Array(
[0] => Array( [fcmToken] => 123 )
[1] => Array( [fcmToken] => )
[2] => Array( [fcmToken] => 789 )
)
Expected Results
Array(
[0] => Array( [fcmToken] => 123 )
[1] => Array( [fcmToken] => 789 )
)
Look at how you can get the wrong output:
Bad Method:
Output:
The zeros got swallowed up!
This is how it should be done:
Instead, you should use
strlen()
to check the values:Method #1: foreach()
Method #2:
array_filter()
w/ anonymous function andarray_values()
for consistencyOutput for either method:
Demonstration of bad method and two good methods.
You need to do it using
array_map
,array_filter
andarray_values
. Check below code :Try using array_map() to apply the filter to every array in the array.
Here we are using
foreach
to iterate over values and using$value
fornon-empty $value["fcmToken"]
Output:
The best and easy single line solution for filter multidimensional array like below.
I hope this solution is work for you. for more detail please visit below link.
PHP: remove empty array strings in multidimensional array
Use
array_filter
with a callback:For checking exactly
null
: