2016-09-05 8 views
0

こんにちはImはtmdb apiを使用し、actorsムービーリストを取得します。var json。コードは私が離れて望む方法で動作します。映画が恐怖の場合は、映画のサムネイルを残りの部分とは異なるスタイルにしています。PHP配列に値が含まれている場合にスタイルを割り当て

$tmdb = $_GET['tmdb']; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://api.themoviedb.org/3/person/".$tmdb."?api_key=#######"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch, CURLOPT_HEADER, FALSE); 
$person = curl_exec($ch); 
curl_close($ch); 
$actor= json_decode($person, true); // Decode the results into an array 
$name = $actor['name']; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://api.themoviedb.org/3/discover/movie?&with_cast=".$tmdb."&vote_count.gte=5&sort_by=release_date.desc&api_key=#######"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch, CURLOPT_HEADER, FALSE); 
$personCredits = curl_exec($ch); 
curl_close($ch); 
$credits= json_decode($personCredits, true); // Decode the results into an array 

$counter4 = 0; 
$counter5 = 0; 
echo "<pre>"; 
print_r($credits); 
echo "</pre>"; 

$page=1; 
echo "<div style='width:800px;overflow:hidden;position:absolute;ktop:1100px;left:75'>"; 

while ($page <= $credits['total_pages']) { 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, "http://api.themoviedb.org/3/discover/movie?&with_cast=".$tmdb."&vote_count.gte=5&sort_by=release_date.desc&page=".$page."&api_key=#######"); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    curl_setopt($ch, CURLOPT_HEADER, FALSE); 
    $personCredits = curl_exec($ch); 
    curl_close($ch); 
    $credits= json_decode($personCredits, true); // Decode the results into an array 

    $resultsCount4=(count($credits['results']))-1; 
    while ($counter4 <= $resultsCount4) { 

     // THIS IS WHERE I NEED HELP PLEASE>>>>> 
     if($credits['results'][$counter4]['genre_ids'] == 27) { 
      $style="horror"; 
     } else { $style="round"; } 
     //////////////////////////////////////////////////// 

     echo "<div style='width:200;height:260;float:left;text-overflow:ellipsis white-space: nowrap; 
         width: 14em; 
         overflow: hidden; 
         '><center><div class='".$style."'><a href=\"/?id=moviepage.php&ttno=".$credits['results'][$counter4]['id']."\"><img width=\"154\" height=\"231\"onerror=\"this.src='nocover.png'\" src='http://image.tmdb.org/t/p/w154/".$credits['results'][$counter4]['poster_path']."'></div><br>".$credits['results'][$counter4]['title']."<br>".$credits['results'][$counter4]['release_date']."</center></a></div>"; 
     $counter4++; 
    } 
    $page++; 
    $counter4=0; 
} 

JSON配列が

Array 
     (
     [page] => 1 
    [results] => Array 
    (
     [0] => Array 
      (
       [poster_path] => /ceVMgY3TzLrfEpaMfaOnPDYnfqA.jpg 
       [adult] => 
       [overview] => John Wick is forced out of retirement by a former associate looking to seize control of a shadowy international assassins’ guild Bound by a blood oath to aid him, Wick travels to Rome and does battle against some of the world’s most dangerous killers. 

       [release_date] => 2017-02-10 
       [genre_ids] => Array 
        (
         [0] => 28 
        ) 

       [id] => 324552 
       [original_title] => John Wick: Chapter Two 
       [original_language] => en 
       [title] => John Wick: Chapter Two 
       [backdrop_path] => /6TPIMjoyRKCKhCGeGigP99qQTWw.jpg 
       [popularity] => 1.714191 
       [vote_count] => 7 
       [video] => 
       [vote_average] => 9.57 
      ) 

     [1] => Array 
      (
       [poster_path] => /l9Eu1e3qNvFSvi66WtHFBoHIgeT.jpg 
       [adult] => 
       [overview] => A defense attorney works to get his teenage client acquitted of murdering his wealthy father. 
       [release_date] => 2016-06-10 
       [genre_ids] => Array 
        (
         [0] => 18 
         [1] => 53 
        ) 

       [id] => 331583 
       [original_title] => The Whole Truth 
       [original_language] => en 
       [title] => The Whole Truth 
       [backdrop_path] => /1pwF12CCtUSmCifDGhZHohp5qYu.jpg 
       [popularity] => 1.875005 
       [vote_count] => 6 
       [video] => 
       [vote_average] => 6.67 
      ) 

のように見え、事前

+0

は 'foreach'を使用することを学ぶ使用します。 – Barmar

答えて

0

genre_idsでのおかげで(名前が複数であることを告知し、「真実」は、2つのジャンルである)の配列です、数字ではないので、

if($credits['results'][$counter4]['genre_ids'] == 27) 

は動作しません。あなたは `$クレジット[ '結果'] [$ COUNTER4]`を繰り返し続ける必要はありませんので、in_array()

if (in_array(27, $credits['results'][$counter4]['genre_ids']) 
+0

それは完全にあなたの助けに感謝を働かせます:) –

関連する問題