if i filter an array with array_filter to eliminate null values, keys are preserved and this generated "holes" in the array. Eg:
The filtered version of [0] => 'foo' [1] => null [2] => 'bar' is [0] => 'foo' [2] => 'bar'
How can i get, instead
[0] => 'foo' [1] => 'bar'
?
The array_filter() function filters the values of an array using a callback function. This function passes each value of the input array to the callback function. If the callback function returns true, the current value from input is returned into the result array. Array keys are preserved.
To use the PHP array_filter() function to filter array elements by key instead of value, you can pass the ARRAY_FILTER_USE_KEY flag as the third argument to the function. This would pass the key as the only argument to the provided callback function.
To filter an array in PHP, use the array_filter() method. The array_filter() takes an array and filter function and returns the filtered array. The filter function is a custom user-defined function with its logic and based on that, it filters out the array values and returns them.
You could use array_values
after filtering to get the values.
Using this input:
$array=['foo',NULL,'bar',0,false,null,'0',''];
There are a few ways you could do it. Demo
It's slightly off-topic to bring up array_filter
's greedy default behavior, but if you are googling to this page, this is probably important information relevant to your project/task:
var_export(array_values(array_filter($array))); // NOT GOOD!!!!!
Bad Output:
array ( 0 => 'foo', 1 => 'bar', )
Now for the ways that will work:
Method #1: (array_values()
, array_filter()
w/ !is_null()
)
var_export(array_values(array_filter($array,function($v){return !is_null($v);}))); // good
Method #2: (foreach()
, auto-indexed array, !==null
)
foreach($array as $v){ if($v!==null){$result[]=$v;} } var_export($result); // good
Method #3: (array_walk()
, auto-index array, !is_null()
)
array_walk($array,function($v)use(&$filtered){if(!is_null($v)){$filtered[]=$v;}}); var_export($filtered); // good
All three methods provide the following "null-free" output:
array ( 0 => 'foo', 1 => 'bar', 2 => 0, 3 => false, 4 => '0', 5 => '', )
From PHP7.4, you can even perform a "repack" like this: (the splat operator requires numeric keys)
Code: (Demo)
$array = ['foo', NULL, 'bar', 0, false, null, '0', '']; $array = [...array_filter($array)]; var_export($array);
Output:
array ( 0 => 'foo', 1 => 'bar', )
... but as it turns out, "repacking" with the splat operator is far less efficient than calling array_values()
.
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