2015-08-28 111 views
1

我想解析PHP中的JSON對象。我的努力導致以此var_dump結束:PHP JSON對象解析

string(5) "Malta" 
string(2) "mt" 
string(11) "St Julian's" 
string(5) "Malta" 
string(2) "mt" 
string(11) "St Julian's" 

現在我想要的只是打印「馬耳他」。所有嘗試的努力都失敗了,我需要提示下一步該做什麼。任何想法如何我可以去做這件事?由於

試圖解析這部分:「位置」:

{ 
"totalFound":2, 
"content":[ 
{ 
"id":"a1d17bwqeqewqeqweaf-1e54-4861-92e1-8246baba11d6", 
"title":"Developer", 
"refNumber":"REFqweqwe4N", 
"createdOn":"2015-08-28T11:10:07.000Z", 
"updatedOn":"2015-08-28T13:19:59.000Z", 
"location":{ 
"country":"Malta", 
"countryCode":"mt", 
"city":"St Julian's" 
}, 
"status":"SOURCING", 
"actions":{ 
"details":{ 
"url":"www.google.com", 
"method":"GET" 
} 
} 
} 
], 
"offset":0, 
"limit":10 
} 

我的代碼片段:

$jfo = json_decode($data, TRUE); 

foreach ($jfo['content'] as $category) { 
if (isset($category['title']) != null) { 
} 
if (isset($category['location']) != null) { 



    foreach ($category['location'] as $location){ 
     var_dump($location); 
     print_r($location); 
+0

可我得到該對象的print_r的? – Buddhi741

+0

看起來更像'var_dump'在循環內部而不是解碼對象? –

+0

@喬恩是的,我也這麼認爲 – Buddhi741

回答

1

嘗試呼應$category['location']['country'] 刪除每個上有 其綁到循環您位置數組

+0

試過了,沒有用 - 它只打印第一個字符 – progdoc

+0

@progdoc check now – Buddhi741

1

問題是您正在遍歷location中的每個元素,即countrycountryCodecity

這確實你需要:

$jfo = json_decode($data, TRUE); 

foreach ($jfo['content'] as $content) 
{ 
    echo $content['location']['country']; 
}