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

i'm trying to build a Media Library website using php. i have 3 categories (books , movies and music ) that i included in the same array like this :

    <?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"

];

?>

and then i displayed in the home page just the books category

"category" => "Books",

using this code :

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

                    }
                }

            ?>
            </ul>

the get_item_html function is this :

<?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;

}

?>

Now all the books items will be displayed in the Home page but i want Just four of them to be displayed randomly.

so what i want is to display 4 items of the array randomly. but they all should have the key and value of : "category" => "Books",.

thank you in advance .

How about this:

array_rand(array,number)

array Required. Specifies an array

number Optional. Specifies how many random keys to return

It returns an array, if number is greater than 1.

You might implement it, like this:

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

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

Shuffle the array and add a counter:

<?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;
        }
    }
?>