数组php中出现COUNT

时间:2016-12-16 11:52:52

标签: php arrays count

我有一个php数组,我想计算id出现的count_giga_winz属性增加的数量

array(
(int) 0 => array(
    'g' => array(
        'id' => '13',
        'photoCadeau' => 'gigawinz-mini-cooper.jpg',
        'photoDetail' => 'giga2-hover.png'
    ),
    (int) 0 => array(
        'count_giga_winz' => '1'
    )
),
(int) 1 => array(
    'g' => array(
        'id' => '14',
        'photoCadeau' => 'w3.jpg',
        'photoDetail' => 'w3.jpg'
    ),
    (int) 0 => array(
        'count_giga_winz' => '1'
    )
),
(int) 2 => array(
    'g' => array(
        'id' => '14',
        'photoCadeau' => 'w3.jpg',
        'photoDetail' => 'w3.jpg'
    ),
    (int) 0 => array(
        'count_giga_winz' => '1'
    )
)
)

我想通过id计算,我看到重复的问题,但它对我不起作用

1 个答案:

答案 0 :(得分:0)

谢谢各位朋友,我使用此代码解决了这个问题:

        $tpl_1= array();
        $tpl= array();
        foreach ($gigawinzs as $keys => $values) {
            $tpl_1[$keys] = array(
                'id'=>$values['g']['id'],
                'photoDetail' => $values['g']['photoDetail'],
                'photoCadeau'=>$values['g']['photoCadeau'],
                'count_giga_winz' => $values[0]['count_giga_winz']
            );
        }
        $sumArray = array();

        foreach ($tpl_1 as $k=>$subArray) {
            @$sumArray[$subArray['id']]+=$subArray['count_giga_winz'];
            $tpl[$subArray['id']] = array(
                'id'=>$subArray['id'],
                'photoDetail' => $subArray['photoDetail'],
                'photoCadeau'=>$subArray['photoCadeau'],
                'count_giga_winz' => $sumArray[$subArray['id']]
            );
        }