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

zhai斋
  • 95

$data =[

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

我需要统计arr1的值为高的有多少个,低的有多少个。然后需要得出占总数的百分比,
数组形式。
$arr=[

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

];
好吧,因为问题提的不明确,修改很多次。十分抱歉!

回复
阅读 9.6k
5 个回答
上官元恒
  • 8.7k
✓ 已被采纳
$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)
*/
anyluck
  • 2
新手上路,请多包涵

要这么难吗?
$one['period']=isset(array_count_values(array_column($arr,'policy_status'))[0])?array_count_values(array_column($arr,'policy_status'))[0]:0;
下表0 是你要统计的数次数

    $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;
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;

请把问题尽量提的好一些吧=。=,数组写的不大走心啊。

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