从特定数量的数组中计数值并返回出现

我有一个二维数组,如下所示:


$array1 = array

   (

   array(1, "Harry", "Male", 30),

   array(2, "Troy", "Male", 4),

   array(3, "Vicky", "Female", 22),

   );

我想返回第四个“列”超过固定数目10的次数


因此,例如,我追逐以下内容:


MalesUnder10 = 

MalesOver10 = 

FemalesUnder10 = 

MalesOver10 = 

谢谢,


呼啦一阵风
浏览 119回答 2
2回答

蝴蝶刀刀

您可以array_filter用来过滤数组。使用count计数结果$array1 = array (&nbsp; &nbsp;array(1, "Harry", "Male", 30),&nbsp; &nbsp;array(2, "Troy", "Male", 4),&nbsp; &nbsp;array(3, "Vicky", "Female", 22),);$MalesUnder10 = count(array_filter( $array1, function( $o ){&nbsp; &nbsp; return $o[2] == "Male" && $o[3] < 10;}));$MalesOver10 = count(array_filter( $array1, function( $o ){&nbsp; &nbsp; return $o[2] == "Male" && $o[3] > 10;}));$FemalesUnder10 = count(array_filter( $array1, function( $o ){&nbsp; &nbsp; return $o[2] == "Female" && $o[3] < 10;}));$FemalesOver10 = count(array_filter( $array1, function( $o ){&nbsp; &nbsp; return $o[2] == "Female" && $o[3] > 10;}));这将导致:$MalesUnder10 = 1$MalesOver10 = 1$FemalesUnder10 = 0$FemalesOver10 = 1注意: 10岁以下的人不会指望逻辑。您可以通过$o[3] => 10或更改它$o[3] =< 10
打开App,查看更多内容
随时随地看视频慕课网APP