5

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.

user663561
  • 190
  • 1
  • 2
  • 11
  • 1
    What did your code look like with shuffle? It should have worked. maybe with a sample set of 3, you just happened to be unlucky and it shuffled back into the same order. Did you do a shuffle($bb) or shuffle($bb['slides'])? – Leniency Mar 17 '11 at 02:32
  • Should it be shuffle($bb['slides'])? – rcravens Mar 17 '11 at 02:33
  • My implementation with mt_rand better than shuffle() http://stackoverflow.com/a/43532529/960020 – Stalingrad Apr 21 '17 at 01:49

6 Answers6

10

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']);
Valera Tumash
  • 628
  • 7
  • 16
Meberem
  • 937
  • 7
  • 18
2

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

shuffle($bb['slides']);
foreach($bb['slides'] as $b):
mario
  • 144,265
  • 20
  • 237
  • 291
1
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
}
Chris
  • 54,599
  • 30
  • 149
  • 186
  • His foreach loop looks correct. Err...aside from the colon. :) – New Guy Mar 17 '11 at 02:36
  • @New Guy - You're right if $bb['slides'] is an array. To me it appeared that he was accessing a single element ('slides') and attempting to foreach that instead. I.e. I thought $bb was the array he was attempting to loop. – Chris Mar 17 '11 at 02:38
  • And actually his colon is correct too. Didn't notice he was using the alternative syntax for control structures. Don't see that very often anymore. – New Guy Mar 17 '11 at 02:43
  • @New Guy - You're right, there is nothing wrong with : syntax, my foreach loop was to emphasize the outer vs inner loop. The braces simply came from habit – Chris Mar 17 '11 at 02:45
1
<?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

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" />';
}
?>
Porta Shqipe
  • 766
  • 7
  • 8
0

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

Brian
  • 3,571
  • 7
  • 44
  • 70