2 回答
TA贡献1801条经验 获得超8个赞
您可以array_filter用来过滤数组。使用count计数结果
$array1 = array (
array(1, "Harry", "Male", 30),
array(2, "Troy", "Male", 4),
array(3, "Vicky", "Female", 22),
);
$MalesUnder10 = count(array_filter( $array1, function( $o ){
return $o[2] == "Male" && $o[3] < 10;
}));
$MalesOver10 = count(array_filter( $array1, function( $o ){
return $o[2] == "Male" && $o[3] > 10;
}));
$FemalesUnder10 = count(array_filter( $array1, function( $o ){
return $o[2] == "Female" && $o[3] < 10;
}));
$FemalesOver10 = count(array_filter( $array1, function( $o ){
return $o[2] == "Female" && $o[3] > 10;
}));
这将导致:
$MalesUnder10 = 1
$MalesOver10 = 1
$FemalesUnder10 = 0
$FemalesOver10 = 1
注意: 10岁以下的人不会指望逻辑。您可以通过$o[3] => 10或更改它$o[3] =< 10
- 2 回答
- 0 关注
- 118 浏览
添加回答
举报