php array_filter without key preservation

前端 未结 2 1004
闹比i
闹比i 2020-12-29 18:24

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
           


        
相关标签:
2条回答
  • 2020-12-29 18:54

    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().

    0 讨论(0)
  • 2020-12-29 19:05

    You could use array_values after filtering to get the values.

    0 讨论(0)
提交回复
热议问题