2015-09-28 43 views
3

我從json獲得一個數組,但我不知道如何獲得bookTitle和url。 如何讀取數組中的這些項目?我如何獲得JSON在PHP中

$data = file_get_contents('C:\Users\rsanchro\Desktop\ftp brasil4-9-2015\moderna_plus-historia\MPCH-C05-P1-1.0.1-ZIP-PT\resource.json'); 
$datos = json_decode($data, true); 

print_r($datos);die; 

Results: 
    Array 
    (
     [topBarColor] => #000000 
     [bookId] => 2aef55bf3a9dec377b4f16a8048c7f84 
     [bookTitle] => História 
     [unitId] => MPCH-C05-P1 
     [unitTitle] => 108-127-MPCH-C05-P1-M 
     [unitResources] => Array 
      (
       [0] => Array 
        (
         [type] => 03 
         [url] => resources/mphis_c06av_escultura_ontem.mp4 
         [urlToShowInsideUnit] => #/compId/id0a33b2a0471e0f444bc0a88b5bb55714 
         [title] => A escultura ontem e hoje 
         [page] => 123 
        ) 

      ) 

    ) 
+0

可能重複http://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) –

回答

1

使用此

 print_r(utf8_decode($datos['bookTitle'])); 
     if(is_array($datos['unitResources']) && count($datos['unitResources']) > 0){ 
      foreach($datos['unitResources'] as $key => $value){ 
       print_r($value['url']); 

      } 
     } 
+0

完美!感謝大家的迴應! – Arts

0

,如果你只單一的數據使用以下

$bookTitle=$datos['bookTitle']; 
$url=$datos['unitResources'][0]['url']; 

如果你有一個以上的數據數組中,然後使用以下

foreach($datos as $dato){ 
    $bookTitle=$dato['bookTitle']; 
    $urls=array(); 
    foreach($datos['unitResources'] as $url){ 
    $urls[]=$url['url']; 
    } 
    echo '<br>Book Title :'.$bookTitle; 
    echo '<br>urls :'; 
    foreach($urls as $url){ 
    echo $url.'<br>'; 
    } 
} 

For your exa你提到的多個數組。你可以使用第一種方法。 但由於你正在使用數組更好地使用第二種方法。

0

試試這個

echo "Title:".$datos['bookTitle']; //To print book title 

    //To print url 
    if(is_array($datos['unitResources']) && count($datos['unitResources']) > 0){ 
       foreach($datos['unitResources'] as $key => $value){ 
        echo "URL:".$value['url'] ; 
       } 
    }