是否有一种简单的方法来计算多维数组中值的出现次数?
我有以下数组:
array(
(int) 0 => array(
'HrEmployee' => array(
'id' => '7',
'surname' => 'Basson',
'name' => 'Andries'
),
'Setup' => array(
'color' => '004356',
'size' => 12
),
(int) 1 => array(
'HrEmployee' => array(
'id' => '25',
'surname' => 'Coetzee',
'name' => 'Zandereen',
),
'Setup' => array(
'color' => 'CFCFCF',
'size' => 8
),
(int) 2 => array(
'HrEmployee' => array(
'id' => '30',
'surname' => 'De Kock',
'name' => 'Theo',
),
'Setup' => array(
'color' => 'FFFFFF',
'size' => 12
),
(int) 3 => array(
'HrEmployee' => array(
'id' => '31',
'surname' => 'De Vaal',
'name' => 'Chantell',
),
'Setup' => array(
'color' => '00FF00',
'size' => 10
),
)
我想检查一定大小的出现次数,所以它应该返回:
array(
(int) 8 => (int) 1,
(int) 10 => (int) 1,
(int) 12 => (int) 2
)
我该如何做到这一点?
答案 0 :(得分:1)
应该像这样工作:
$sizes = array();
foreach($arr as $v) {
$s = $v['Setup']['size'];
if(!isset( $sizes[$s])){
$sizes[$s] = 0;
}
$sizes[$s]++;
}
print_r($sizes);