簡體   English   中英

JSON結果是stdObject,但如何遍歷呢?

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

我將數據存儲在一個變量中,當輸出看起來像這樣時

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" } 

問題是我如何到達每個單獨的元素。 我嘗試將對象轉換為數組,但這是否必要?

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

為什么這不起作用,即不給變量賦值:

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