PHP - 在php中显示具有特定键和值的关联数组的四个项目

时间:2016-03-23 14:16:17

标签: php arrays loops associative-array

我正在尝试使用php构建媒体库网站。 我有3个类别(书籍,电影和音乐),我包括在同一个数组中:

    <?php 

$catalog = [];
$catalog[101] = [
    "title" => "Book1",
    "img" => "img/img1.jpg",
    "genre" => "genre1",
    "year" => "2016",
    "category" => "Books",
    "author" => "someone"

];
$catalog[102] = [
    "title" => "Book2",
    "img" => "img/img1.jpg",
    "genre" => "genre1",
    "year" => "2016",
    "category" => "Books",
    "author" => "someone"

];
$catalog[103] = [
    "title" => "Book3",
    "img" => "img/img1.jpg",
    "genre" => "genre1",
    "year" => "2016",
    "category" => "Books",
    "author" => "someone"

];
$catalog[104] = [
    "title" => "Book4",
    "img" => "img/img1.jpg",
    "genre" => "genre1",
    "year" => "2016",
    "category" => "Books",
    "author" => "someone"

];
$catalog[105] = [
    "title" => "Book5",
    "img" => "img/img1.jpg",
    "genre" => "genre1",
    "year" => "2016",
    "category" => "Books",
    "author" => "someone"

];
$catalog[201] = [
    "title" => "Movie1",
    "img" => "img/img2.png",
    "genre" => "genre1",
    "year" => "2016",
    "category" => "Movies",
    "actor" => "someone"

];
$catalog[202] = [
    "title" => "Movie2",
    "img" => "img/img2.png",
    "genre" => "genre1",
    "year" => "2016",
    "category" => "Movies",
    "actor" => "someone"

];
$catalog[203] = [
    "title" => "Movie3",
    "img" => "img/img2.png",
    "genre" => "genre1",
    "year" => "2016",
    "category" => "Movies",
    "actor" => "someone"

];
$catalog[204] = [
    "title" => "Movie4",
    "img" => "img/img2.png",
    "genre" => "genre1",
    "year" => "2016",
    "category" => "Movies",
    "actor" => "someone"

];
$catalog[301] = [
    "title" => "Song1",
    "img" => "img/img3.png",
    "genre" => "genre1",
    "year" => "2016",
    "category" => "Music",
    "singer" => "someone"

];
$catalog[302] = [
    "title" => "Song2",
    "img" => "img/img3.png",
    "genre" => "genre1",
    "year" => "2016",
    "category" => "Music",
    "singer" => "someone"

];
$catalog[303] = [
    "title" => "Song3",
    "img" => "img/img3.png",
    "genre" => "genre1",
    "year" => "2016",
    "category" => "Music",
    "singer" => "someone"

];
$catalog[304] = [
    "title" => "Song4",
    "img" => "img/img3.png",
    "genre" => "genre1",
    "year" => "2016",
    "category" => "Music",
    "singer" => "someone"

];

?>

然后我在主页上显示了书籍类别

  

“category”=&gt; “书”,

使用此代码:

<ul class="item">
           <?php 
               foreach($catalog as $id => $tem){
                if ($tem["category"] == "Books"){
                    echo get_item_html($id,$tem);

                    }
                }

            ?>
            </ul>

get_item_html函数是这样的:

<?php
function get_item_html($id,$tem){
$output = "<li><a href='#'><img class='img' src='" . $tem["img"] . "' 
alt='" . $tem["title"] . "' />" . "<p>view details</p>" . "</a></li>";

return $output;

}

?>

现在所有书籍项目都会显示在主页中,但我想要随机显示其中的四个。

所以我想要的是随机显示4个数组项目。但它们都应该具有以下关键和价值:“category”=&gt; “书”,

提前谢谢。

2 个答案:

答案 0 :(得分:0)

这个怎么样:

array_rand(array,number)

array必填。指定数组

number可选。指定要返回的随机密钥数

如果number大于1,则返回一个数组。

您可以实现它,如下所示:

function is_book($value){
    return $value["category"] == "Books"
}

$random_books_catalog = array_rand(array_filter( $catalog , "is_book"), 4);

答案 1 :(得分:0)

随机播放阵列并添加一个计数器:

<?php
    $nOfBooks = 4;
    $countBooks = 0;
    shuffle($catalog);
    foreach ($catalog as $id => $tem) {
        if ($tem["category"] == "Books" && $countBooks < $nOfBooks) {
            echo get_item_html($id, $tem);
            $countBooks++;
        } elseif ($countBooks >= $nOfBooks){
            // avoiding unnecessary iterations
            break;
        }
    }
?>