$stmt = $web_dbi->prepare($query);
$stmt->execute();
$result = $stmt->get_result();
$num_of_rows = $result->num_rows;
while($row=mysqli_fetch_array($result)){
$results[] = $row;
}
$randomNumber = mt_rand(0,($num_of_rows-1));
if($results[$randomNumber]['mysql_field']==anothervalue){
...
如何随机访问$results[]
的给定元素?在这种情况下,这种语法是否正确?
答案 0 :(得分:2)
您的代码看起来很好。我个人更喜欢使用array_rand从数组中选择一个随机元素:
$random_value = $results[array_rand($results)];