2017-09-22 120 views
1

不適用於嵌套元素 輸出第一級的元素,並且不讀取帶數據的嵌套數組,因爲可以獲取值 - id,標題和位置?使用嵌套元素解析json

<?php 

function removeBomUtf8($s){ 
    if(substr($s,0,3)==chr(hexdec('EF')).chr(hexdec('BB')).chr(hexdec('BF'))){ 
     return substr($s,3); 
    }else{ 
     return $s; 
    } 
} 

$url = "https://denden000qwerty.000webhostapp.com/opportunities.json"; 
$content = file_get_contents($url); 
$clean_content = removeBomUtf8($content); 
$decoded = json_decode($clean_content); 

while ($el_name = current($decoded)) { 
// echo 'total = ' . $el_name->total_items . 'current = ' . $el_name->current_page . 'total = ' . $el_name->total_pages . '<br>' ; 
    echo ' id = ' . $el_name->data[0]->id . ' title = ' . $el_name->data.title . ' location = ' . $el_name->data.location . '<br>' ; 
    next($decoded); 
} 
?> 
+0

什麼是要分析的東西? –

+0

「data」:[{「id」:412235,「title」:「我們說英語」,「lat」:「10.6966159」,「lng」:「-74.8741045」,「url」:「https:// gis .aiesec.org/v2/opportunities/412235「,」status「:」open「,」current_status「:」open「,」location「:」哥倫比亞Sabanalarga「,」城市「:」哥倫比亞特區「, 「programs」:{「id」:1,「short_name」:「GV」},「applications_count」:54,「is_favourited」:false,「branch」:{「id」:321136,「name」:「@UNIATLÁNTICO 「,」組織「:等等...... – NormalArs

+0

我需要得到 - 」id「:412235,」title「:」我們說英語「city」:「哥倫比亞Atlântico」 – NormalArs

回答

2

$el_name->data[0]->id是正確的

$el_name->data.title

你看有什麼區別?

$decoded是根(無需遍歷它) - 要遍歷data孩子

<?php 
foreach($decoded->data as $data) 
{ 
    $id = (string)$data->id; 
    $title = (string)$data->title; 
    $location = (string)$data->location; 


    echo sprintf('id = %s, title = %s, location = %s<br />', $id, $title, $location); 
}