具有空多子字段检查的数据阵列

时间:2016-12-02 11:43:03

标签: php arrays

我有一个问题,我的数据阵列有大约一千条记录,一些数据行如下:

$data = array(
[0]=array("id"=>2,"name"=>'John',"status"=>1,"comment"=>""),
[1]=array("id"=>3,"name"=>'rock',"status"=>1),
[2]=array("id"=>4,"comment"=>""),
[3]=array("id"=>5,"name"=>'Jack',"status"=>1),
[4]=array("id"=>6,"name"=>'Scott',"status"=>0),
[5]=array("id"=>7,"name"=>'Nathan',"status"=>1),
[6]=array("id"=>8,"name"=>'locus',"status"=>1)
[7]=array("id"=>9,"comment"=>""),
[8]=array("id"=>10,"report"=>""),
[9]=array("id"=>12,"name"=>''),
[10]=array("id"=>20,"name"=>'Sairr',"status"=>1)
);

我只想忽略具有子字段1的所有行,如

  

[7] =阵列( “ID”=> 9)

Moy代码:

foreach( $data as $field )
{
         //code
         //code
         //code
} 

我发布了样本数据,但我的实际数据非常大,但情况相同。 谢谢你提前。请帮助我。

1 个答案:

答案 0 :(得分:1)

sizeof / count函数会为您提供数组的大小(元素数)。

使用array_filter

$data = array(
   array("id"=>2,"name"=>'John',"status"=>1),
   array("id"=>3,"name"=>'rock',"status"=>1),
   array("id"=>4),
   array("id"=>8,"name"=>'locus',"status"=>1),
   array("id"=>9),
   array("id"=>20,"name"=>'Sairr',"status"=>1),
);
$array = array_filter($data, function($v) { return 1 < sizeof($v); });
print_r($array);

或简单if - 条件:

foreach ($data as $item) {
    if (1 < count($item)) {
        print_r($item);
    }
}