dsx5201 2019-03-30 06:21
浏览 36

如何在php中显示所有数组?

I would like to display all categories randomly along with collections also randomly.

$array_category[]=array("category"=>"MIEJSCA","collection"=>"Miasta","collection"=>"Uzdrowiska polskie");
    $array_category[]=array("category"=>"SZTUKA","collection"=>"Łódzkie Witraże");
    $array_category[]=array("category"=>"KRAJOBRAZ","collection"=>"Karkonosze");
    $array_category[]=array("category"=>"MIASTA","collection"=>"Łódź Nocą");
    $array_category[]=array("category"=>"ZAMKI","collection"=>"Szlakiem Orlich Gniazd","collection"=>"Polskie Zamki Gotyckie");
shuffle( $array_category );
$randNumKat = rand( 1, count( $array_category ) );
foreach ( $array_category as &$thiskat) 
{                                   
 echo '<div class="kat">
    <a href="?kategoria='.$thiskat["category"].'">'
     .$thiskat["category"].
    '</a>
     <kol>
        <kol_naz>
       <a href="?kolekcja='.$thiskat["collection"].'">
        '.$thiskat["collection"].'
       </a>
      </kol_naz>
    </kol>
 </div>';
}
unset( $thiskat );

This code displays the categories correctly, but I do not know how to display the collections correctly by category. Please help.

  • 写回答

1条回答 默认 最新

  • douchou8935 2019-03-30 06:55
    关注

    Using the proper structure

    Rather than just an array, you likely need an array of objects. Each object would then contain both a category and a nested array of collections.

    I won't do all the work for you as it is better for you to learn, but try running the following code to see how it works:

    $array = [
        (object)[
            'category' => 'MIEJSCA',
            'collections' => [
                'Miasta',
                'Uzdrowiska polskie'
            ]
        ],
        (object)[
            'category' => 'SZTUKA',
            'collections' => [
                'Łódzkie Witraże'
            ]
        ],
        (object)[
            'category' => 'KRAJOBRAZ',
            'collections' => [
                'Karkonosze'
            ]
        ],
        (object)[
            'category' => 'MIASTA',
            'collections' => [
                'Łódź Nocą'
            ]
        ],
        (object)[
            'category' => 'ZAMKI',
            'collections' => [
                'Szlakiem Orlich Gniazd',
                'Polskie Zamki Gotyckie'
            ]
        ]
    ];
    
    $random = rand(0, count($array) - 1);
    
    $category = $array[$random]->category;
    $collections = $array[$random]->collections;
    
    echo "Category is $category
    ";
    
    foreach($collections as $collection){
        echo " Collection is $collection
    ";
    }
    

    An indexing issue with rand()

    One other issue was with your rand() function as follows:

    • Arrays in PHP start with index 0. Therefore if you have 5 elements in your array, the indexes are 0 to 4.
    • Your rand() function was giving a random number between 1 and 5 instead of 0 and 4. See my corrected code.

    You do not seem to be using your rand function in the above code, but thought I would mention it.

    Displaying all categories and all collections in random order

    If you are wanting to display all categories and all collections, just in random order you will need to change my code as follows:

    • Do not use a rand() function at all
    • Use shuffle outside an outer foreach loop
    • The outer foreach loop should loop through all $array elements

    Inside the outer loop do the following:

    • display the category
    • do a shuffle on the collections
    • use another foreach loop to loop through each collection item

    Inside the inner loop do the following:

    • display the collection item
    评论

报告相同问题?

悬赏问题

  • ¥15 python变量和列表之间的相互影响
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)