我已经在 php 页面上运行了 1000 次 foreach 循环。foreach 循环内的代码如下所示:
$first = mysql_query("SELECT givenname FROM first_names order by rand() LIMIT 1");
$first_n = mysql_fetch_array($first);
$first_name = $first_n['givenname'];
$last = mysql_query("SELECT surname FROM last_name order by rand() LIMIT 1");
$last_n = mysql_fetch_array($last);
$last_name = $last_n['surname'];
$first_lastname = $first_name . " " . $last_name;
$add = mysql_query("SELECT streetaddress FROM user_addresss order by rand() LIMIT 1");
$addr = mysql_fetch_array($add);
$address = $addr['streetaddress'];
$unlisted = "unlisted";
$available = "available";
$arr = array(
$first_lastname,
$address,
$unlisted,
$available
);
然后我一直在使用 array_rand 函数在每次循环运行时获取一个随机值:
<td><?php echo $arr[array_rand($arr)] ?></td>
所以加载 php 页面需要很长时间。有没有办法优化这段代码。因为每次循环运行时我都需要一个唯一值