In PHP, you can shuffle an array using the shuffle function.
In this example,
intArray
containing integers.shuffle
function to shuffle intArray
.<?php
$intArray = [1, 2, 3, 4, 5];
shuffle($intArray);
echo 'Shuffled Array: ' . json_encode($intArray);
?>
Shuffled Array: [3,2,5,1,4]
In this example,
strArray
containing strings.shuffle
function to shuffle strArray
.<?php
$strArray = ['apple', 'banana', 'cherry'];
shuffle($strArray);
echo 'Shuffled Array: ' . json_encode($strArray);
?>
Shuffled Array: ['banana','apple','cherry']
In this tutorial, we learned How to Shuffle an Array in PHP language with well detailed examples.