首页 > 解决方案 > How do I iterate list of dictionary in PHP?

问题描述

How do I iterate list of dictionary returned from curl response in PHP?

Here is structure of a returned response:

Array ( 
  [0] => Array ( 
    [status]                      => open 
    [serviceSpecification]        => https://testdomain.com 
    [expirationDate]              => 
    [name]                        => abcd.com 
    [service]                     => servicekey 
    [domainName]                  => domainname 
    [productInstanceUrl]          => https://anotherinstanceurl.com 
    [createDate]                  => 2019-04-15 
    [serviceSpecificationTextKey] => test.core.key 
    [billingCycle]                => 1 
  ) 
  [1] => Array ( 
    [status]                      => open 
    [serviceSpecification]        => https://test.net 
    [expirationDate]              => 
    [name]                        => testname 
    [service]                     => https://service.com 
    [domainName]                  => test 
    [productInstanceUrl]          => https://instanceurl.com 
    [createDate]                  => 2019-04-15 
    [serviceSpecificationTextKey] => core.test.key 
    [billingCycle]                => 1 
  ) 
)

I tried doing following but not working:

foreach ($aboveVariable as $record) {
   echo $record['domainName']; 
}

Note: I believe its more of a how to iterate through list of list in PHP?

标签: phploopsmultidimensional-array

解决方案


仅显示domainNamefor each 然后您显示的代码有效。但基于此,我相信它更多的是如何遍历 PHP 中的列表列表?我会说你只想循环主数组,然后是子数组:

foreach($aboveVariable as $record) {
    foreach($record as $name => $value) {
        echo "$name = $value<br>\n"; 
    }
    echo "<br>\n";
}

将显示如下内容:

status = open
serviceSpecification = https://testdomain.com
etc...

status = open
serviceSpecification = https://test.net
etc...

推荐阅读