Choose a random string from an array and print it together with the previous and the next string from the array (PHP) -
I have an array, its name is
$ swflist
Now when I want to select a random string from the array, I will use it
$ random = mt_rand (0, $ no);
$ Flash = $ Saddestist [$ Random];
print_r ($ Flash);
But then I'm not able to get the next and previous entries from the array - how can I select a random string from the array, and then print the next and previous string from the array So that I can print it like this (tells that the random string is 7)
< P> Using print_r (current ($ swflist)); And so does not work, the pointer just stays in the beginning of the array.Previous: 6
Current: 7 Next: 8
innovative solution:
$ random = mt_rand (0, $ No); $ Flashes = $ sophisticated [$ random]; $ Prev = $ swflist [$ random - 1]; $ Next = $ hesitation [$ random + 1];
The problem is whether $ 0 is a random one? Do you want to go to the end of the array, etc. ...
The reason for this is not that it works that the use of the array does not set the internal indicator if you want to do this, you To access the randomly selected number you will need to use the next, and then do the last / next. See for more information.
With the comments, you can do something like set before and after --- and then override if they are legitimate.
$ Random = mt_rand (0, $ no); $ Flashes = $ sophisticated [$ random]; $ Prev = '---'; $ Next = '---'; If ($ Random> 0) {$ prev = $ swflist [$ random - 1]; } If ($ Random + 1 & lt; = Calculation ($ swflist)) {$ next = $ swflist [$ random + 1]; }
Comments
Post a Comment