9

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
(
    [0] => Array
        (
            [id] => 7
            [status] => 1
            [sortorder] => 0
            [title] => Pants
        )

    [1] => Array
        (
            [id] => 8
            [status] => 1
            [sortorder] => 0
            [title] => Jewels
        )

    [2] => Array
        (
            [id] => 9
            [status] => 1
            [sortorder] => 0
            [title] => Birdhouse
        )

    [3] => Array
        (
            [id] => 10
            [status] => 1
            [sortorder] => 0
            [title] => Shirt
        )

    [4] => Array
        (
            [id] => 11
            [status] => 1
            [sortorder] => 0
            [title] => Phone
        )

)

// This how the result is if I use array_rand()
print_r(array_rand($slides, 5));

Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
)

// This how the result is if I use shuffle()
print_r(shuffle($slides));

1
1

4 Answers 4

23

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Up vote on "Don't just assume how they work" because php functions are inconsistent as hell :)
Palm to head Of course! I misunderstod the explanation at php.net for shuffle()
I feel like this is a dumb comment but I assume "shirt" wants to maintain its "status" after the shuffle. In other words, only the first dimension get shuffled, right?
While print_r works in the class room, in real world, when you need to use echo, use $slides[0][1]. One (1) is the part of the array to return. This is not obvious in the PHP manual. In other words, you wish to echo the first array (0) and the part needed
4

Instead of

print_r(shuffle($slides));

do

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

You see shuffle() shuffles the array in-place

1 Comment

Thanks for all the downvotes. A comment on what's wrong would be nice though
1

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.