如何在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.

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