# 统计二维数组中某个值出现的次数

zhai斋
• 95

\$data =[

``````        '0' =>[

'id' => 7,
'patient_id' => 1,
'arr1' => 高,
],
'1' =>[
'id' => 11,
'patient_id' => 1,
'arr1' => 低,
],
'2' => [
'id' => 13,
'patient_id' => 1,
'arr1' => 低,
]
];``````

\$arr=[

``````'0'=>[
'arr1'=>高，
'sl'=>1,
'bfb'=>33.33%,
],
'1'=>[
'arr1'=>低，
'sl'=>2,
'bfb'=>66.66%,
]``````

];

5 个回答

• 8.6k
✓ 已被采纳
``````\$word = ["高","低","正常"];
\$data = [
'0' =>[
'id' => 7,
'patient_id' => 1,
'arr1' => "高",
],
'1' =>[
'id' => 11,
'patient_id' => 1,
'arr1' => "低",
],
'2' => [
'id' => 13,
'patient_id' => 1,
'arr1' => "低",
]
];
\$count = array_count_values(array_column(\$data,"arr1"));
\$wordCount = [];
foreach(\$word as \$k => \$v)
{
\$sl = isset(\$count[\$v])?\$count[\$v]:0;
\$wordCount[] = [
'arr1'=>\$v,
'sl'=>\$sl,
'bfb'=>(round(\$sl / array_sum(\$count),4) * 100).'%',
];
}
var_dump(\$wordCount);
/*
//输出
array (size=3)
0 =>
array (size=3)
'arr1' => string '高' (length=3)
'sl' => int 1
'bfb' => string '33.33%' (length=6)
1 =>
array (size=3)
'arr1' => string '低' (length=3)
'sl' => int 2
'bfb' => string '66.67%' (length=6)
2 =>
array (size=3)
'arr1' => string '正常' (length=6)
'sl' => int 0
'bfb' => string '0%' (length=2)
*/``````
daryl
• 4.7k
``count(array_search(array_column(\$arr, ‘arr1’), ‘高’));``

Cirtus
• 116
``````
\$arr = [
0 => [
'id' => 7,
'patient_id' => 1,
'arr1' => '高',
],
1 => [
'id' => 11,
'patient_id' => 1,
'arr1' => '低',
],
2 => [
'id' => 13,
'patient_id' => 1,
'arr1' => '低',
]
];
\$i = 0;
\$j = 0;
foreach (\$arr as \$item) {
if (\$item['arr1'] == '高') {
\$i+=1;
}elseif (\$item['arr1'] == '低'){
\$j+=1;
}
}
echo \$i.'</br>'.\$j;``````

``````    \$arr = [
0 => [
'id' => 7,
'patient_id' => 1,
'arr1' => '高',
],
1 => [
'id' => 11,
'patient_id' => 1,
'arr1' => '低',
],
2 => [
'id' => 13,
'patient_id' => 1,
'arr1' => '低',
]
];

\$result = array();
foreach (\$arr as \$value) {
if(empty(\$value)) {
continue;
}
if(!isset(\$result[\$value['arr1']])) {
\$result[\$value['arr1']] = 1;
continue;
}

\$result[\$value['arr1']] ++;
}

var_dump("<pre>", \$result);die;``````
anyluck
• 2

\$one['period']=isset(array_count_values(array_column(\$arr,'policy_status'))[0])?array_count_values(array_column(\$arr,'policy_status'))[0]:0;

###### 你尚未登录，登录后可以
• 和开发者交流问题的细节
• 关注并接收问题和回答的更新提醒
• 参与内容的编辑和改进，让解决方法与时俱进