显示数组数组的随机值

时间:2014-08-06 13:13:36

标签: php kohana

下面是查询构建器生成的数组。

   $random_array =  Array ( [0] => Array ( [text] => A great time was had by all! ) 
            [1] => Array ( [text] => KILL SHOT ) 
            [2] => Array ( [text] => How is it possible) 
            [3] => Array ( [text] => http://www.youtube.com/watch?v=KwGOZpbxU9g ) 
            [4] => Array ( [text] => http://www.youtube.com/watch?v=KwGOZpbxU9g ) 
          )

目前我这样做是为了打印随机值

print_r(array_rand($random_array,1));

这是将数组键打印为3或1等(从上面的数组中随机)。我想打印键的值而不是键。

例如,我想打印像"http://www.youtube.com/watch?v=KwGOZpbxU9g" or "A great time was had by all!"这样的随机值,而不是现在正在打印的3 or 1

是否可以这样做。

2 个答案:

答案 0 :(得分:3)

您将再添加一行代码,如下所示:

$array_key = array_rand($random_array,1); //get the key
print_r( $random_array[$array_key] );     //use the key to print value

答案 1 :(得分:0)

如何简单地调用

$randNumber = rand(0,count($random_array))-1; //index in array starts with 0    
print (string) $random_array[$randNumber];