循环遍历一组嵌套的stdClass对象和数组

时间:2015-04-04 17:16:54

标签: php arrays foreach stdclass

我正在尝试遍历stdClass对象($ args),其格式如下所示。目标是循环遍历每个sObject,JSON编码值并将其发送到端点。

我认为这样可以解决问题:

$object = $args->Notification->sObject;

    foreach($object as $item) { JSON_ENCODE AND SEND TO PASTEB.IN }

除了我得到'空'回应。

如果我这样做:

$object = $args->Notification;

    foreach($object as $item) {

        $sf = $item->sObject;"

        // JSON_ENCODE $sf and send to PASETBI.N

        }

...我得到的结果只有一个结果发布到pasteb.in,而不是所有结果(例如3个单独的结果,如下表所示)。

我已经看过关于循环遍历数组/ sObjects的其他问题的答案,但我无法做到正确。任何帮助将不胜感激!

Print_r'ing此对象显示以下格式:

stdClass Object
(
    [OrganizationId] => XXXXXXXXXXXXXXXX
    [ActionId] => XXXXXXXXXXXXXXXX
    [SessionId] => 
    [EnterpriseUrl] => https://darlston-dev-ed.my.salesforce.com/services/Soap/c/33.0/XXXXXXXXXXXXXXXX
    [PartnerUrl] => https://darlston-dev-ed.my.salesforce.com/services/Soap/u/33.0/XXXXXXXXXXXXXXXX
    [Notification] => Array
        (
            [0] => stdClass Object
                (
                    [Id] => XXXXXXXXXXXXXXXX
                    [sObject] => stdClass Object
                        (
                            [Id] => XXXXXXXXXXXXXXXX
                            [Contact_Email__c] => test@testemail.com
                        )

                )

            [1] => stdClass Object
                (
                    [Id] => XXXXXXXXXXXXXXXX
                    [sObject] => stdClass Object
                        (
                            [Id] => XXXXXXXXXXXXXXXX
                            [Contact_Email__c] => test@testemail.com
                        )

                )

            [2] => stdClass Object
                (
                    [Id] => XXXXXXXXXXXXXXXX
                    [sObject] => stdClass Object
                        (
                            [Id] => XXXXXXXXXXXXXXXX
                            [Contact_Email__c] => test@testemail.com
                        )

                )

        )

)

1 个答案:

答案 0 :(得分:0)

你能试试吗?

foreach($args->Notification as $item)
{
    echo $item->Id."<br>";
    echo $item->sObject->Id."<br>";
    echo $item->sObject->Contact_Email__c."<br>";
}