迭代通过stdClass对象数组

时间:2012-12-18 13:07:28

标签: arrays foreach loops stdclass

对于PHP,我非常非常新手,但慢慢学习:) 我有一个函数,它返回一个带有多个数组的stdClass对象(参见下面的摘录)。

我想用foreach循环遍历它并使用某些值进行一些计算。有人能指出我的方向吗?

**摘录输出print_r $ result **

stdClass Object
(
    [processor] => stdClass Object
        (
            [code] => 0
            [message] => OK
            [rid] => 289411
            [testmode] => 1
        )

    [handler] => stdClass Object
        (
            [statistics] => Array
                (
                    [0] => Array
                        (
                            [eventdate] => data
                            [eventstatus] => data
                            [programid] => data
                            [programname] => data
                            [eventcurrency] => data
                            [clicks] => data
                        )
                    [1] => Array
                        (
                            [eventdate] => data
                            [eventstatus] => data
                            [programid] => data
                            [programname] => data
                            [eventcurrency] => data
                            [clicks] => data
                        )

                        )




                    )

我想要做的是添加所有[点击]值,然后打印所有[点击]值的总和。

非常感谢你!

1 个答案:

答案 0 :(得分:1)

试试这个:

$clicks = 0;
foreach($result->handler->statistics as $arrData){
    //calc like this
    $clicks += $arrData['clicks'];
}
echo "Clicks: " . $clicks;