0

print_r($rows); results in the following:

Array
(
    [0] => Array
        (
            [bg_image] => uploads/2013/06/Home_background1.jpg
        )

    [1] => Array
        (
            [bg_image] => uploads/2013/06/Home_background2.jpg
        )

    [2] => Array
        (
            [bg_image] => uploads/2013/06/Home_background3.jpg
        )

)

What I'm looking to get help with is randomly selecting one of the values from above. I'm fairly new to php so sorry if this is a basic question.

1

4 Answers 4

7
echo $rows[array_rand($rows)]['bg_image'];
Sign up to request clarification or add additional context in comments.

1 Comment

with only a couple entries speed won't matter, but here is a quick benchmark report showing rand() would be faster: ebrueggeman.com/blog/php_benchmarking_array_rand
0

You can use the rand function to select a random index. The following will give you a random index that will be either 0, 1, or 2. Using that as the index to the array will output one of the 3 elements at random.

rand(0,2)

Comments

0

use rand() function.

refer here

PHP.net rand()

Comments

0

A different way from the other answers

$rand = mt_rand(0,2);
echo $rows[$rand]['bg_image'];

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.