在PHP中随机播放数组 [英] Shuffle an array in PHP

查看:79
本文介绍了在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']);

这篇关于在PHP中随机播放数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆