array filter null php 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: array filter use key
$my_array = ['foo' => 1, 'hello' => 'world'];
$allowed = ['foo', 'bar'];
$filtered = array_filter(
$my_array,
function ($key) use ($allowed) {
return in_array($key, $allowed);
},
ARRAY_FILTER_USE_KEY
);