简体   繁体   English

JSON结果是stdObject,但如何遍历呢?

[英]JSON result is an stdObject but how to traverse it?

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? 我尝试将对象转换为数组,但这是否必要?

foreach ($data as $object)
{
  foreach ($object as $property=>$value)
   {
     echo $property." has the value ". $value;
   } 
}

Why would this not work, ie 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;
     }
   } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM