如果php数组包含值,则指定样式

时间:2016-09-05 22:09:49

标签: php css json styles themoviedb-api

您好我正在使用tmdb api并获取一个演员电影列表var json。代码的工作方式除了我试图将电影缩略图与其他电影缩略图区分开来时,如果电影是恐怖电影的话。

$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
            )

提前致谢

1 个答案:

答案 0 :(得分:0)

genre_ids是一个数组(注意名称是复数,“整个真相”有两种类型),而不是数字,所以

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

不起作用。使用in_array()

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