-4

Is there a simple way to get the result of this foreach loop shuffled on output at each page load?

foreach(glob('images/fotoalbum/*.*') as $file)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Muiter
  • 1,470
  • 6
  • 26
  • 39
  • 2
    `$files = glob('images/fotoalbum/*.*'); shuffle($files); foreach($files as $file)` Something like this? – Dave Chen Dec 16 '17 at 15:08
  • can you show an example of the result you want to achieve? – Claudio Dec 16 '17 at 15:08
  • @DavidChen https://meta.stackexchange.com/questions/230676/hey-you-yeah-you-post-your-answers-as-answers-not-comments/296481#296481 – mickmackusa Dec 16 '17 at 15:11
  • @Muiter where is your coding attempt to shuffle? – mickmackusa Dec 16 '17 at 15:11
  • @mickmackusa I believe the OP should close their own question. The example I provided is the [first example on the manual](http://php.net/manual/en/function.shuffle.php#refsect1-function.shuffle-examples). This is a common case of just RTM. – Dave Chen Dec 16 '17 at 15:12
  • Did you do ANY research before posting? RTM? https://stackoverflow.com/questions/37528644/php-shuffle-multiple-loop – mickmackusa Dec 16 '17 at 15:15
  • Most of you have a degree in scripting or working daily with it. I don't. I did not realize that that a glob function also returns an 'normal' array that can be shuffled. – Muiter Dec 16 '17 at 15:19
  • 1
    Did you try? I don't have a degree. Did you even bother to look in the manual? – mickmackusa Dec 16 '17 at 15:20
  • If you don't know that the glob function returns an array then you are not searching to shuffle an array. – Muiter Dec 16 '17 at 15:22
  • Again, RTM -- `glob()` is in there and it tells you all about it. – mickmackusa Dec 16 '17 at 15:24

1 Answers1

1

you can't shuffle after output but you can before.

try this

$dir=glob('images/fotoalbum/*.*');
shuffle($dir);
foreach($dir as $file){
//operation here
}
jasinth premkumar
  • 1,430
  • 1
  • 12
  • 22
  • 1
    Please do not answer this poor question. – mickmackusa Dec 16 '17 at 15:15
  • why man ? is it duplicate – jasinth premkumar Dec 16 '17 at 15:15
  • DEFINITELY A DUPLICATE "How to shuffle an array?" Come on! But I have been here long enough to know that when low rep users get an upvote, they aren't going to delete their answer. DavidChen could have answered this before you, but didn't. Why do you think that is? – mickmackusa Dec 16 '17 at 15:15
  • should i delete? why? – jasinth premkumar Dec 16 '17 at 15:21
  • 1
    You no longer can because the OP has accepted your answer. SO continues to rot. When users ask questions without putting for ANY effort, they lean on SO like a crutch. They never get better, they never learn. SO gets more and more bloated with duplicate questions that must be moderated and closed/deleted. This means WASTED volunteer time. This all part of horrible cycle where people are being selfish and/or lazy. – mickmackusa Dec 16 '17 at 15:23