2012-04-04 81 views
0

一個隨機值,我有這個數組PHP如何從陣列

$row4 = mysql_fetch_array($sql_res);

誰能幫助我如何獲得的$row4['response']隨機值?

$row4沒有固定值...
但現在$row4包含

Array ([0] => 3 [id] => 3 [1] => where is dryad [react] => where is dryad [2] => Dryad is found in the farthest part of the Dark wilderness. [response] => Dryad is found in the farthest part of the Dark wilderness. [3] => [review] =>) 1 
+0

什麼包含$ row4 ['response']? – Salepate 2012-04-04 00:58:45

+3

請參閱http://php.net/array-rand – Pete 2012-04-04 00:58:46

+1

或者你想要一個隨機的行嗎? http://stackoverflow.com/questions/19412/how-to-request-a-random-row-in-sql – alberge 2012-04-04 01:00:50

回答

3

當你的代碼是正確的,現在,你只會收到回一個查詢一行。該行只包含您該行的字段值。如果您確實只是想從單排隨機字段值,你可以使用:

$randomKey = array_rand($row4,1); 

如果你的意思是要求從您的查詢排隨機,你可以做兩個方面,這一個:

1)使用array_rand搶排隨機,並放入$randomRow

while($row = mysql_fetch_array($sql_res)) $rows[] = $row; 
$randomRow = array_rand($rows); 

2)在您的查詢,您可以指定只搶到1個隨機行,而不是每一個結果:

SELECT col1 FROM tbl ORDER BY RAND() LIMIT 1;

+0

先生你能告訴我如何.. ..獲得隨機行的響應值?我只想要隨機響應 – 2012-04-04 01:13:07

+0

我試過$ randomKey ['response']但沒有結果.... – 2012-04-04 01:13:39

+0

如果您使用的是$ randomKey方法,意味着您沒有收到一個數組,只有您的一個隨機值行返回。如果你想引用一個隨機行(這將是一個數組),使用其他方法之一。 – VictorKilo 2012-04-04 03:28:01