0

有没有一种简单的方法来计算多维数组中某个值的出现次数?

我有以下数组:

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
)

我怎样才能做到这一点?

4

1 回答 1

1

应该像这样工作:

$sizes = array();
foreach($arr as $v) {
    $s = $v['Setup']['size'];
    if(!isset( $sizes[$s])){
        $sizes[$s] = 0;
    }
    $sizes[$s]++;
}

print_r($sizes);
于 2013-08-02T08:46:02.200 回答