php - 如果 php 数组包含值,则分配样式

标签 php css json styles themoviedb-api

您好,我正在使用 tmdb api 并获取 Actor 电影列表 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
            )

提前致谢

最佳答案

genre_ids 是一个数组(注意名字是复数,《The Whole Truth》分两个流派),不是数字,所以

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

不会工作。使用 in_array()

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

关于php - 如果 php 数组包含值,则分配样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39338215/

相关文章:

Json 数组到 Pojo

android - modelmapper 是为在 android 中使用而设计的吗?

php - Laravel EventListener 不会触发

javascript - CSS 或 JS 被覆盖

javascript - 单击 Font Awesome 图标时如何计算数字?

html - 文本区域和文本缩进 : placeholder doesn't work properly on EDGE

iphone - 如何在 iOS 上将图片以 JSON 格式从图库上传到服务器

php - Laravel 表单文本未上传到数据库

php - 简单 URL 路由不适用于 CodeIgniter

php include 在切换到 SSL 时不起作用