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?

lbholland
  • 83
  • 1
  • 4
  • 10

1 Answers1

0

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

$image = $fields[array_rand($fields)]['homepage_image'];
Jake Wilson
  • 88,616
  • 93
  • 252
  • 370