Tuesday, April 5, 2011

php array_filter without key preservation

Hi,

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'

? Thanks

From stackoverflow
  • You could use array_values after filtering to get the values.

    ILMV : +1, exactly what I was thinking

0 comments:

Post a Comment