0

我在 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
                        )

                        )




                    )

例如,我想做的是将所有 [clicks] 值相加,然后打印所有 [clicks] 值的总和。

非常感谢您提前!

4

1 回答 1

1

尝试这个:

$clicks = 0;
foreach($result->handler->statistics as $arrData){
    //calc like this
    $clicks += $arrData['clicks'];
}
echo "Clicks: " . $clicks;
于 2012-12-18T13:11:21.087 回答