如何显示多维数组。

时间:2022-08-27 18:04:51

I have a multidimensional array that i have pulled back from salesforce using their API:

我有一个多维数组,我用他们的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
)  

I also did a var_dump:

我还做了一个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"
} 

I am using the code below to display the Name: Marcellus House, but am struggling to display the other 2 fields in the array [AccountId] => 0015800000XXXX12 and [Name] => Test Client

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

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

I would like to all the data as follows.

我希望所有的数据如下。

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

But struggling to get this to work... Any help would be much appreciated...

但要想让这一切正常运转……如有任何帮助,我们将不胜感激。

2 个解决方案

#1


0  

You need to add one more 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;
}

#2


0  

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

This should work. The Account id and Name are further nested in this object. I edited your post to make it much more readable, in fact I would ALWAYS suggest you use var dump surronded by a pre element, like this:

这应该工作。帐户id和名称进一步嵌套在这个对象中。我编辑了你的文章,让它更容易阅读,事实上,我总是建议你使用前元素的var转储,像这样:

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

Then you can see the structure and simple build the path you need to go to access the information you need. In this case you have to "go" to Contacts, then records. Since records is an array with only the index 0 you can simply say records[0] and "go" to the object stored in it which contains your desired information.

然后您可以看到结构和简单的构建路径,您需要访问您需要的信息。在这种情况下,你必须“去”联系,然后记录。因为记录是一个只有索引0的数组,所以您可以简单地说记录[0]和“go”到存储在其中的对象,其中包含您想要的信息。

If this doe snot help you please let me know.

如果这只母鹿不帮你,请告诉我。

#1


0  

You need to add one more 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;
}

#2


0  

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

This should work. The Account id and Name are further nested in this object. I edited your post to make it much more readable, in fact I would ALWAYS suggest you use var dump surronded by a pre element, like this:

这应该工作。帐户id和名称进一步嵌套在这个对象中。我编辑了你的文章,让它更容易阅读,事实上,我总是建议你使用前元素的var转储,像这样:

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

Then you can see the structure and simple build the path you need to go to access the information you need. In this case you have to "go" to Contacts, then records. Since records is an array with only the index 0 you can simply say records[0] and "go" to the object stored in it which contains your desired information.

然后您可以看到结构和简单的构建路径,您需要访问您需要的信息。在这种情况下,你必须“去”联系,然后记录。因为记录是一个只有索引0的数组,所以您可以简单地说记录[0]和“go”到存储在其中的对象,其中包含您想要的信息。

If this doe snot help you please let me know.

如果这只母鹿不帮你,请告诉我。