在PHP中随机播放数组

问题描述:

我有以下代码:

<?php
foreach($bb['slides'] as $b):
$url = "domain.com/" . $b->image . ";
echo($url);
endforeach;
?>

输出如下: domain.com/image1.jpg domain.com/image2.jpg domain.com/image3.jpg

The output is as follows: domain.com/image1.jpg domain.com/image2.jpg domain.com/image3.jpg

我正在尝试将输出顺序随机化.在foreach语句之前,我尝试使用shuffle($ bb);来对数组进行混洗.但这没有用.感谢您的帮助.

I am trying to randomize the order of the output. Before the foreach statement I tried to shuffle the array using shuffle($bb); but that did not work. Any help is appreciated.

由于$ bb是一个数组数组,shuffle()不会将子数组随机化,请对嵌套数组尝试shuffle,如下所示:

As $bb is an array of arrays, shuffle() won't randomise the sub-array, try shuffle on the nested array as follows:

shuffle($bb['slides']);