Remove lines with empty values from multidimensional-array in php

后端 未结 5 1426
忘了有多久
忘了有多久 2021-01-21 23:50

How do you remove the lines that have empty elements from a multidimensional-array in PHP?

For instance, from:

1: a, b, c, d
2: d, _, b, a
3: a, b, _, _
         


        
5条回答
  •  有刺的猬
    2021-01-22 00:16

    Use this code:

    $source = array(
        array('a', 'b', 'c', 'd'),
        array('d', '_', 'b', 'a'),
        array('a', 'b', '_', '_'),
        array('d', 'c', 'b', 'a'),
        array('_', 'b', 'c', 'd'),
        array('d', 'c', 'b', 'a'),
    );
    
    $sourceCount = count($source);
    
    for($i=0; $i<$sourceCount; $i++)
    {
      if(in_array("_", $source[$i])) unset($source[$i]);
    }
    

    See: http://ideone.com/Vfd6Z

提交回复
热议问题