填充后$author_array, 你需要从中随机挑选6个用户。这里有两个简单的选择:
洗牌数组shuffle($author_array); 并使用for或while lopp从中弹出值array_pop()使用创建数组六个元素的随机副本array_rand($author_array,6); 然后像上面那样使用foreach遍历新的随机数组就我个人而言,我更喜欢#2,但请注意,如果您尝试从少于6个元素的数组中选择6个随机元素array_rand() 你会得到警告的。您需要测试$author_array 具有count($author_array) 首先限制array_rand() 命令设置为潜在作者列表的最大大小,如果为1,则完全跳过它。
比如,在你的第一个foreach结束后:
if(count($author_array) >= 6) {
    $max = 6;
} else {
    $max = count($author_array);
}
$random_authors = array_rand($author_array,$max);
foreach ($random_authors as $author) { ...