multi dimensional array in random order

后端 未结 4 1620
谎友^
谎友^ 2020-12-02 01:43

I want to make it so that my multi dimensional array is in a random order. How would you do it?

// This is how the array looks like
print_r($slides);

Array
         


        
相关标签:
4条回答
  • 2020-12-02 02:22

    It works perfect. print_r(shuffle($slides))) gives the output of TRUE, since the return value of shuffle is a boolean and not an array.

    See the working example here: http://codepad.org/B5SlcjGf

    0 讨论(0)
  • 2020-12-02 02:27

    shuffle() is the way to go here. It prints 1 because shuffle changes the array in-place and returns a boolean, as it is written in the documentation:

    Returns TRUE on success or FALSE on failure.

    I suggest to also read the documentation of array_rand():

    Picks one or more random entries out of an array, and returns the key (or keys) of the random entries.


    Always read documentation if you use built-in functions. Don't just assume how the work. I bet it took more time to write the question than looking this up.

    0 讨论(0)
  • 2020-12-02 02:27

    i am not sure how you want it to display but you can loop the array and use php rand(0,arraylen) function to parse the array.

    0 讨论(0)
  • 2020-12-02 02:32

    Instead of

    print_r(shuffle($slides));
    

    do

    shuffle($slides);
    print_r($slides);
    

    You see shuffle() shuffles the array in-place

    0 讨论(0)
提交回复
热议问题