PHP remove array keys with empty values code example
Example 1: php array remove empty values
<?php
$arr = array('1', '', '2', '3', '0');
print_r(array_filter($arr));
print_r(array_filter($arr, 'strlen'));
print_r(array_filter($arr, function ($val) {if ($val > 0) {return true;} else {return false;}}));
Example 2: php remove empty values from array
$array = array_filter($array, function($a) {return $a !== "";});
$array = array_filter($array, function($a) {
return trim($a) !== "";
});
Example 3: remove key value pair if value is empty php
print_r(array_filter($linksArray, fn($value) => !is_null($value) && $value !== ''));
print_r(array_filter($linksArray, function($value) { return !is_null($value) && $value !== ''; }));
print_r(array_filter($linksArray, create_function('$value', 'return $value !== "";')));