Q:

remove null values from array php

<?php
$arr = array('1', '', '2', '3', '0');
// Incorrect:
print_r(array_filter($arr));
// Correct:
print_r(array_filter($arr, 'strlen'));
//Custom
print_r(array_filter($arr, function ($val) {if ($val > 0) {return true;} else {return false;}}));
5
// PHP 7.4 and later
print_r(array_filter($linksArray, fn($value) => !is_null($value) && $value !== ''));

// PHP 5.3 and later
print_r(array_filter($linksArray, function($value) { return !is_null($value) && $value !== ''; }));

// PHP < 5.3
print_r(array_filter($linksArray, create_function('$value', 'return $value !== "";')));
2
$colors = array("red","","blue",NULL);

$colorsNoEmptyOrNull = array_filter($colors, function($v){ 
 return !is_null($v) && $v !== ''; 
});
//$colorsNoEmptyOrNull is now ["red","blue"]
0
array_filter
0

New to Communities?

Join the community