JSON 结果是一个 stdObject 但如何遍历它?

发布于 2024-10-21 14:53:38 字数 628 浏览 2 评论 0原文

我将数据放在一个变量中,当输出看起来像这样时,

object(stdClass)#1 (20) { ["Link"]=> object(stdClass)#2 (2) { ["Text"]=> string(30) "A.Ilkley Moor Medical Practice" ["Uri"]=> string(87) "http://v1.syndication.nhschoices.nhs.uk/organisations/gppractices/25599?apikey=LDVVTHBM" } ["Address"]=> object(stdClass)#3 (6) { ["Line1"]=> string(28) "Ilkley Moor Medical Practice" ["Line2"]=> string(17) "The Health Centre" ["Line3"]=> string(12) "Springs Lane" ["Line4"]=> string(6) "Ilkley" ["Line5"]=> string(0) "" ["Postcode"]=> string(7) "LS298TH" } 

问题是如何获取每个单独的元素。我尝试过将对象转换为数组,但这有必要吗?

I have my data in a variable which when output looks something like this

object(stdClass)#1 (20) { ["Link"]=> object(stdClass)#2 (2) { ["Text"]=> string(30) "A.Ilkley Moor Medical Practice" ["Uri"]=> string(87) "http://v1.syndication.nhschoices.nhs.uk/organisations/gppractices/25599?apikey=LDVVTHBM" } ["Address"]=> object(stdClass)#3 (6) { ["Line1"]=> string(28) "Ilkley Moor Medical Practice" ["Line2"]=> string(17) "The Health Centre" ["Line3"]=> string(12) "Springs Lane" ["Line4"]=> string(6) "Ilkley" ["Line5"]=> string(0) "" ["Postcode"]=> string(7) "LS298TH" } 

Question is how do I get to each individual element. I have tried converting the object to an array but is this necessary?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一曲爱恨情仇 2024-10-28 14:53:38
foreach ($data as $object)
{
  foreach ($object as $property=>$value)
   {
     echo $property." has the value ". $value;
   } 
}
foreach ($data as $object)
{
  foreach ($object as $property=>$value)
   {
     echo $property." has the value ". $value;
   } 
}
呆萌少年 2024-10-28 14:53:38

为什么这不起作用,即不给变量赋值:

foreach (json_decode($data) as $object)
{
  foreach ($object as $property=>$value)
   {
     echo $property." has the value ". $value;
     switch ($property) {
        case 'Text':            
            $practice_name = $value;
            break;
        case 'Uri':
            $Website = $value;          
            break;
        case 'Line1':
            $Address1 = $value;
            break;
     }
   } 
}

Why would this not work, i.e. not assign values to the variables :

foreach (json_decode($data) as $object)
{
  foreach ($object as $property=>$value)
   {
     echo $property." has the value ". $value;
     switch ($property) {
        case 'Text':            
            $practice_name = $value;
            break;
        case 'Uri':
            $Website = $value;          
            break;
        case 'Line1':
            $Address1 = $value;
            break;
     }
   } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文