print_r($rows); results in the following:
的print_r($行);结果如下:
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.
我正在寻求帮助的是随机选择上面的一个值。我对php很新,很抱歉,如果这是一个基本问题。
4 个解决方案
#1
7
echo $rows[array_rand($rows)]['bg_image'];
#2
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,1或2的随机索引。使用它作为数组的索引将随机输出3个元素中的一个。
rand(0,2)
#4
0
A different way from the other answers
与其他答案不同的方式
$rand = mt_rand(0,2);
echo $rows[$rand]['bg_image'];
#1
7
echo $rows[array_rand($rows)]['bg_image'];
#2
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,1或2的随机索引。使用它作为数组的索引将随机输出3个元素中的一个。
rand(0,2)
#3
#4
0
A different way from the other answers
与其他答案不同的方式
$rand = mt_rand(0,2);
echo $rows[$rand]['bg_image'];