从stdClass对象数组中提取数据

时间:2016-02-15 02:54:07

标签: php

我正在对Salesforce进行api调用,print_r($ response)返回以下结果

QueryResult Object
(
    [queryLocator] => 
    [done] => 1
    [records] => Array
        (
            [0] => stdClass Object
                (
                    [Id] => 0018000001O5tRgAAJ
                    [Contacts] => stdClass Object
                        (
                            [done] => 1
                            [queryLocator] => 
                            [records] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [Id] => 0038000001yxYP3AAM
                                            [Email] => mktest3@gmail.com
                                            [FirstName] => mary
                                            [LastName] => kell
                                        )

                                )

                            [size] => 1
                        )

                    [Name] => mktest3
                )

        )

我可以使用以下php脚本从第一个stdClass对象数组中挑选出某些数据,但是如何从第三个进行相同的操作呢?基本上我正在尝试获取联系人的ID,Email,FirstName和LastName值。

foreach ($response->records as $record) {
    $sObject = new SObject($record);

    echo "<p>$sObject->Id</p>";
    echo "<p>$sObject->Name</p>";
}

1 个答案:

答案 0 :(得分:0)

此处已提供解决方案:Convert stdClass object to array in PHP

以下内容将响应转换为标准数组

$array = json_decode(json_encode($response), True);