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

报告相同问题?

悬赏问题

  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?