2012-03-28 33 views
0

我正在使用api作爲電影日期和im試圖解析json數組。香港專業教育學院試圖解析的發行日期,但我收到此錯誤 - 致命錯誤:無法使用字符串數組致命錯誤:不能使用字符串偏移量作爲數組 - 從JSON響應解析字符串

下面偏移的例子從陣列

Array 
(
    [total] => 17 
    [movies] => Array 
     (
      [0] => Array 
       (
        [id] => 22494 
        [title] => Titanic (in 3D) 
        [year] => 1997 
        [mpaa_rating] => PG-13 
        [runtime] => 195 
        [critics_consensus] => A mostly unqualified triumph for Cameron, who offers a dizzying blend of spectacular visuals and old-fashioned melodrama. 
        [release_dates] => Array 
         (
          [theater] => 2012-04-04 
          [dvd] => 1999-08-31 
         ) 

這裏是我所收到的錯誤的簡單代碼。

<?php 
$url = 'http://api.rottentomatoes.com/api/public/v1.0/lists/movies/upcoming.json?apikey=px8rr7zr5c6qjwpea66gdf93&page_limit=18'; 
$json = file_get_contents($url); 
$data = json_decode($json, TRUE); 

foreach($json['releasedates']['theatre'] as $item) { 
    print $item['theatre']; 
} 

?> 

理想我想分析的日期,一個變量,並能夠將它們

由於比較當前日期的幫助球員:)

回答

2

foreach使用了錯誤的變量(你拼寫錯了劇場,按照數據)

編輯: 您的腳本,工作:

<?php 
    $url = 'http://api.rottentomatoes.com/api/public/v1.0/lists/movies/upcoming.json?apikey=px8rr7zr5c6qjwpea66gdf93&page_limit=18'; 
    $json = file_get_contents($url); 
    $data = json_decode($json, TRUE); 

    foreach($data['movies'] as $item) { 
     echo $item['title'] . ' is opening on ' . $item['release_dates']['theater'] . "\n"; 
    } 
+0

這不適用於提供的數據。 '$ json ['releasedates']'是一個包含一個元素的數組,'2011-07-29'',這個''影院''有一個鍵。 – 2012-03-28 20:11:23

+0

我剛剛編輯併發布了一個工作腳本......他發佈的數據與他提供的腳本中的url返回的內容不匹配... – keithhatfield 2012-03-28 20:13:11

+0

感謝您的回答,它的完美。我將如何去分析返回的日期,以便它會顯示日期之間的差異。例如泰坦尼克號在戒指之前4天被釋放?我應該發佈一個單獨的問題嗎? – user1064660 2012-03-28 20:21:43

相關問題