Here’s how you can declare an array in plain PHP, shuffle it, and traverse it:
<?php
// Declare the array
$fruits = ["Apple", "Banana", "Cherry", "Mango", "Orange"];
// Shuffle the array (random order)
shuffle($fruits);
// Traverse the shuffled array
foreach ($fruits as $fruit) {
echo $fruit . "<br>";
}
?>
Explanation:
shuffle($array)
randomly reorders the elements in place.foreach
lets you loop through each element easily.
If you wanted the shuffled result without modifying the original array, you could use:
<?php
$fruits = ["Apple", "Banana", "Cherry", "Mango", "Orange"];
// Create a shuffled copy
$shuffled = $fruits;
shuffle($shuffled);
foreach ($shuffled as $fruit) {
echo $fruit . "<br>";
}
?>