aadu
aadu

Reputation: 3254

Echoing the elements of a shuffled array with PHP

I have an array of elements in PHP called...

$completeArray

...and I'm trying to store a randomized version of this array in my session called...

$_SESSION['videoArray']

...so I'm trying something like this...

$_SESSION['videoArray'] = shuffle($completeArray);

...but when I try to echo the first element of this randomized array like this...

$videoid = $_SESSION['videoArray'];
echo $videoid[0];

...all it's returning is the 'key' of the element. How do I randomize the array and be able to echo the actual elements of the new array?

Upvotes: 0

Views: 67

Answers (2)

Greg Motyl
Greg Motyl

Reputation: 2545

You can try somethin like this:

 $_SESSION['videoArray'] = $completeArray;

shuffle($_SESSION['videoArray']);

Upvotes: 0

xdazz
xdazz

Reputation: 160833

shuffle take a reference of an array and Returns TRUE on success or FALSE on failure.

You should do:

shuffle($completeArray);
$_SESSION['videoArray'] = $completeArray;

Upvotes: 4

Related Questions