I have an array
Array ( [0] => 0 [1] => [2] => 3 [3] => )
i want to remove null values from this and the result should be like this
Array ( [0] => 0 [1] => 3) i don't want to remove 0 value from array.
We can remove duplicate element in an array by 2 ways: using temporary array or using separate index. To remove the duplicate element from array, the array must be in sorted order. If array is not sorted, you can sort it by calling Arrays. sort(arr) method.
To remove all null values from an array:Declare a results variable and set it to an empty array. Use the forEach() method to iterate over the array. Check if each element is not equal to null . If the condition is satisfied, push the element into the results array.
this will do the trick:
array_filter($arr, static function($var){return $var !== null;} );
Code Example: https://3v4l.org/jtQa2
for older versions (php<5.3):
function is_not_null ($var) { return !is_null($var); }
$filtered = array_filter($arr, 'is_not_null');
Code Example: http://3v4l.org/CKrYO
You can use array_filter()
which will get rid of the null empty values from the array
print_r(array_filter($arr, 'strlen'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With