2016-04-05 15 views
3

Ich versuche, eine inoffizielle IMDb API für mein Projekt zu verwenden. Ich habe diesen Code benutzt.PHP foreach Schleife in verschachtelten Array

<form name="search-imdb" autocomplete="off" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
<input type="text" name="q" title="Type Name, Title, Character etc and hit Enter" placeholder="Type Name, Title, Character etc and hit Enter" size="50" /> 
<input type="submit" name="submit" title="Click to Fetch Data from IMDb" value="Search IMDb" /> 
</form> 

<?php 

if (isset($_POST['submit'])){ 
    $user_search = $_POST['q']; 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, "imdb.wemakesites.net/api/search?q=".$user_search); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $output = curl_exec($ch); 
    $array = json_decode($output, true); 
    print_r($array); 

    foreach ($array as $key => $data){ 
    if(is_array($data)){ 
     foreach($data as $subkey => $results){ 
      if(is_array($results)){ 
       foreach($results as $key => $titles){ 
        echo $titles."<br />"; 
       } 
      } else { 
       echo $results."<br />"; 
      } 
     } 
    } else { 
     echo $data."<br />"; 
    } 
} 
    curl_close($ch); 
} else { 

} 
?> 

Ich brauche nur die Werte der „Titel“ Array zu erhalten. (title, id, url) Dies war der JSON-Code.

Array{ 
"status": "success", 
"code": 200, 
"message": "ok", 
"term": "jQuery21409989625962002346_1459839763205&q", 
"search_url":"http://www.imdb.com/find?q=batmansuperman&s=all5703b359add1b", 
"data": { 
    "results": { 
     "titles": [ 
      { 
       "title": "Batman", 
       "id": "tt0096895", 
       "url":"http://www.imdb.com/title/tt0096895/" 
      }, 
      { 
       "title": "Batman", 
       "id": "tt0059968", 
       "url":"http://www.imdb.com/title/tt0059968/" 
      }, 
      { 
       "title" :"Batman v Superman: Dawn of Justice", 
       "id": "tt2975590", 
       "url":"http://www.imdb.com/title/tt2975590/" 
      }, 
      { 
       "title": "Batman Begins", 
       "id": "tt0372784", 
       "url":"http://www.imdb.com/title/tt0372784/ 
      } 
     ] 
    } 
} 

Und das ist mein $ Array nach JSON Decoding.

Array ([status] => success [code] => 200 [message] => ok [term] => megamind [search_url] => http://www.imdb.com/find?q=megamind&s=all5703f4f88841f [data] => Array ([results] => Array ([titles] => Array ([0] => Array ([title] => Megamind [id] => tt1001526 [url] => http://www.imdb.com/title/tt1001526/?ref_=fn_al_tt_1) [1] => Array ([title] => Megamind [id] => tt1785464 [url] => http://www.imdb.com/title/tt1785464/?ref_=fn_al_tt_2) [2] => Array ([title] => MegaMind [id] => tt1890468 [url] => http://www.imdb.com/title/tt1890468/?ref_=fn_al_tt_3) [3] => Array ([title] => Mega Mindy [id] => tt0891395 [url] => http://www.imdb.com/title/tt0891395/?ref_=fn_al_tt_4) [4] => Array ([title] => Megamind [id] => tt3022564 [url] => http://www.imdb.com/title/tt3022564/?ref_=fn_al_tt_5) [5] => Array ([title] => Megamind: The Button of Doom [id] => tt1847645 [url] => http://www.imdb.com/title/tt1847645/?ref_=fn_al_tt_6) [6] => Array ([title] => Megamind [id] => tt3624292 [url] => http://www.imdb.com/title/tt3624292/?ref_=fn_al_tt_7) [7] => Array ([title] => Megamind [id] => tt2173285 [url] => http://www.imdb.com/title/tt2173285/?ref_=fn_al_tt_8) [8] => Array ([title] => Mega Mindy Versus ROX [id] => tt4706602 [url] => http://www.imdb.com/title/tt4706602/?ref_=fn_al_tt_9) [9] => Array ([title] => Aa Megamisama [id] => tt0872301 [url] => http://www.imdb.com/title/tt0872301/?ref_=fn_al_tt_10)) [characters] => Array ([0] => Array ([title] => Megamind [id] => ch0194198 [url] => http://www.imdb.com/character/ch0194198/?ref_=fn_al_ch_1) [1] => Array ([title] => Megamind's Mother [id] => ch0229958 [url] => http://www.imdb.com/character/ch0229958/?ref_=fn_al_ch_2) [2] => Array ([title] => Megamind's Father [id] => ch0229956 [url] => http://www.imdb.com/character/ch0229956/?ref_=fn_al_ch_3)) [names] => Array ([0] => Array ([title] => Megamind [id] => nm6292338 [url] => http://www.imdb.com/name/nm6292338/?ref_=fn_al_nm_1)) [keywords] => Array ([0] => Array ([title] => megami [id] => megami [url] => http://www.imdb.com/keyword/megami/?ref_=fn_al_kw_1)) [companies] => Array ([0] => Array ([title] => Megami [id] => co0544394 [url] => http://www.imdb.com/company/co0544394/?ref_=fn_al_co_1) [1] => Array ([title] => Mega Mind Media [id] => co0558438 [url] => http://www.imdb.com/company/co0558438/?ref_=fn_al_co_2) [2] => Array ([title] => Elm Tree Gaming [id] => co0202064 [url] => http://www.imdb.com/company/co0202064/?ref_=fn_al_co_3))))) 

Kann mir jemand sagen, was ich hier falsch mache? Ich bekomme diesen Fehler - "Array zu String Conversion"

Ich habe versucht, dies für mehrere Stunden zu lösen, und ich habe nach einer Antwort über das Internet gesucht. Einige Hilfe wird sehr geschätzt. Vielen Dank.

+0

Was genau tun Sie hier drucken? Das auch in welchem ​​Format? Bitte geben Sie das zuerst an. –

+0

Sie sollten Text anstelle von Bildern für diese Art von Daten verwenden, damit die Benutzer sie problemlos anzeigen und damit arbeiten können. [Auch Ihre Daten scheinen JSON zu sein, dann verwenden Sie 'json_decode ($ data, true);' um es in ein Array zu setzen] (http://php.net/manual/en/function.json-decode.php) – Prix

+0

Zuallererst ... ein Array namens Array ist wie ein Hund namens Hund. Bitte finden Sie einen aussagekräftigen Namen für Ihr Array, mein Freund. –

Antwort

4

Von was ich sehe, das scheint wie ein JSON und kein Array. In diesem Fall sollten Sie dies zuerst tun:

$array = json_decode($array, true); 

Wenn es um den Array-Traversal kommt, können Sie so etwas wie dies versuchen:

foreach($array['data']['results']['titles'] as $data) { 
    echo "Title:".$data['title']; 
    echo "<br/>"; 
    echo "ID:".$data['id']; 
    echo "<br/>"; 
    echo "URL:".$data['url']; 
    echo "<hr/>"; 
} 

Hoffnung, das hilft.

+2

Vielen Dank sooooooo viel :) Danke eine Tonne. Es funktionierte. –

2

dass Angenommen ist JSON-Daten, die Sie json_decode fahren, die data, results und titles sind nicht Array-Schlüssel, sie Objekteigenschaften sind. Versuchen Sie etwas wie $array->data->results->titles[0];. Das Zeichen { bezeichnet eine Objekteigenschaft, während [ ein Array bezeichnet.

2

Verwenden

$json_array = json_encode($array['data']['results']['titles']); 
    print_r($json_array);