drg5577 2016-03-23 14:16
浏览 24
已采纳

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 .

  • 写回答

2条回答 默认 最新

  • doubi7346 2016-03-23 14:35
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?