PHP如何显示多维数组

时间:2016-07-13 08:01:24

标签: php arrays multidimensional-array

我有一个多维数组,我已经使用他们的API从salesforce撤回了:

SObject Object
(
    [type] => [fields] => [Contacts] => stdClass Object
    (
        [done] => 1 [queryLocator] => [records] => Array
        (
            [0] => stdClass Object
            (
                [Id] => [AccountId] => 0015800000XXXX12 [Name] => Test Client
            )
        )
        [size] => 1
    )
    [Name] => Marcellus House
)  

我也做了一个var_dump:

object(SObject)#5 (4)
{
    ["type"]=> NULL
    ["fields"]=> NULL
    ["Contacts"]=> object(stdClass)#7 (4)
    {
        ["done"]=> bool(true)
        ["queryLocator"]=> NULL
        ["records"]=> array(1)
        {
            [0]=> object(stdClass)#8 (3)
            {
                ["Id"]=> NULL
                ["AccountId"]=> string(18) "0015800000XXXX12"
                ["Name"]=> string(16) "Test Client"
            }
        }
        ["size"]=> int(1)
    }
    ["Name"]=> string(15) "Marcellus House"
} 

我使用下面的代码显示名称:Marcellus House,但我正在努力显示数组中的其他2个字段[AccountId] => 0015800000XXXX12和[名称] =>测试客户端

foreach ($Response->records as $RecordSet)
{
    echo $RecordSet->Name;
}

我想要的所有数据如下。

foreach ($Response->records as $RecordSet)
{
    echo $RecordSet->Name;
    echo $RecordSet->AccountId;
    echo $RecordSet->Name;
}

但努力让这个工作......任何帮助都会非常感激......

2 个答案:

答案 0 :(得分:0)

您需要再添加一个foreach

foreach ($Response->records as $RecordSet) 
{
    $accountid = "";
    $name = "";
    foreach($RecordSet->records as $record)
    {
        $accountid = $record['AccountId'];
        $name = $record['Name'];
    }
    echo $RecordSet->Name;
    echo $accountid;
    echo $name;
}

答案 1 :(得分:0)

foreach ($Response->records as $RecordSet)
{
    echo $RecordSet->Name;
    echo $RecordSet->Contacts->records[0]->AccountId;
    echo $RecordSet->Contacts->records[0]->Name;
}

这应该有效。帐户ID和名称进一步嵌套在此对象中。我编辑了你的帖子,使其更具可读性,事实上我总是建议你使用pre pre元素的var dump,如下所示:

echo "<pre>";
var_dump($yourVariable);
echo "</pre>";

然后,您可以看到结构并简单地构建了访问所需信息所需的路径。在这种情况下,你必须&#34;去&#34;到联系人,然后记录。由于记录是一个只有索引0的数组,你可以简单地说记录[0]和&#34;去&#34;存储在其中的包含所需信息的对象。

如果这个小鼻涕帮助你,请告诉我。