PHP 中的数组打乱顺序
我有以下代码:
<?php
foreach($bb['slides'] as $b):
$url = "domain.com/" . $b->image . ";
echo($url);
endforeach;
?>
输出如下: 域名.com/image1.jpg 域名.com/image2.jpg domain.com/image3.jpg
我正在尝试随机化输出的顺序。在 foreach 语句之前,我尝试使用 shuffle($bb); 对数组进行洗牌。但这没有用。任何帮助表示赞赏。
I have the following code:
<?php
foreach($bb['slides'] as $b):
$url = "domain.com/" . $b->image . ";
echo($url);
endforeach;
?>
The output is as follows:
domain.com/image1.jpg
domain.com/image2.jpg
domain.com/image3.jpg
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
由于 $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:当您应该这样做时,您可能会打乱外部 $bb 数组:
You probably shuffled the outer $bb array, when you should have done:
http://www.php.net/manual/en/function.shuffle.php
foreach 也应该是
http://www.php.net/manual/en/function.shuffle.php
Also the foreach should be
查看此博客以获取示例说明。
http://wamp6.com/php/str_shuffle-php/ 检查数组随机播放
Check this blog for explanation with example.
http://wamp6.com/php/str_shuffle-php/ Check for array shuffle
看起来您需要执行
shuffle($bb['slides'])
。Looks like you need to do
shuffle( $bb['slides'] )
.以随机顺序显示内容
以随机顺序显示图像
Display content at random order
Display images at random order