0

I need to select a random item (img1, img2, etc.) from simple nested arrays. I'm sure this is easy but I am stumped. The array has this format:

Array
(
   [0] => Array
        (
            [homepage_image] => img1
        )

    [1] => Array
        (
            [homepage_image] => img2
        )

)

$fields is the name of the main array. I've tried using:

$random = array_rand($fields);

But of course that just gives me 0 or 1. How do I randomly get img1, img2, etc?

1
  • $fields[array_rand($fields)]['homepage_image']; Commented Jun 27, 2014 at 3:24

1 Answer 1

0

Use array_rand() to find a random key of your array:

$image = $fields[array_rand($fields)]['homepage_image'];
Sign up to request clarification or add additional context in comments.

1 Comment

14k, you should know not to post code-only answers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.