PHP 中的数组打乱顺序

发布于 2024-10-22 22:30:47 字数 303 浏览 5 评论 0原文

我有以下代码:

<?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

余生再见 2024-10-29 22:30:47

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

shuffle($bb['slides']);

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']);
悟红尘 2024-10-29 22:30:47

当您应该这样做时,您可能会打乱外部 $bb 数组:

shuffle($bb['slides']);
foreach($bb['slides'] as $b):

You probably shuffled the outer $bb array, when you should have done:

shuffle($bb['slides']);
foreach($bb['slides'] as $b):
暖阳 2024-10-29 22:30:47
shuffle($array_name); // will shuffle array

http://www.php.net/manual/en/function.shuffle.php

foreach 也应该是

for($array_name as $array_item) {
// do stuff
}
shuffle($array_name); // will shuffle array

http://www.php.net/manual/en/function.shuffle.php

Also the foreach should be

for($array_name as $array_item) {
// do stuff
}
玩世 2024-10-29 22:30:47
<?php
shuffle($bb['slides']);
foreach($bb['slides'] as $b) {
    echo $url = "domain.com/" . $b->image . ";
}
?>

查看此博客以获取示例说明。

http://wamp6.com/php/str_shuffle-php/ 检查数组随机播放

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

Check this blog for explanation with example.

http://wamp6.com/php/str_shuffle-php/ Check for array shuffle

情绪失控 2024-10-29 22:30:47

看起来您需要执行shuffle($bb['slides'])

Looks like you need to do shuffle( $bb['slides'] ).

森林很绿却致人迷途 2024-10-29 22:30:47

以随机顺序显示内容

<?php
$myContentList = array (
    'One',
    'Two',
    'Three',
    'Four'
);
shuffle ($myContentList);
foreach ($myContentList as $displayAtRandomOrder) {
echo '<div>' . $displayAtRandomOrder . '</div>';
}
?>

以随机顺序显示图像

<?php
$myImagesList = array (
    'one.png',
    'two.png',
    'three.jpg',
    'four.gif'
);
shuffle ($myImagesList);
foreach ($myImagesList as $displayImagesAtRandomOrder) {
echo '<img src="images/' . $displayImagesAtRandomOrder . '" width="200" height="40" border="0" />';
}
?>

Display content at random order

<?php
$myContentList = array (
    'One',
    'Two',
    'Three',
    'Four'
);
shuffle ($myContentList);
foreach ($myContentList as $displayAtRandomOrder) {
echo '<div>' . $displayAtRandomOrder . '</div>';
}
?>

Display images at random order

<?php
$myImagesList = array (
    'one.png',
    'two.png',
    'three.jpg',
    'four.gif'
);
shuffle ($myImagesList);
foreach ($myImagesList as $displayImagesAtRandomOrder) {
echo '<img src="images/' . $displayImagesAtRandomOrder . '" width="200" height="40" border="0" />';
}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文