After you populate $author_array
, you need to pick 6 random users out of it. Here are two straightforward choices:
- Shuffle the array
shuffle($author_array);
and pop values off of it in a for or while lopp usingarray_pop()
- Create a randomized copy of six elements of the array using
array_rand($author_array,6);
and then iterate through the new randomized array using foreach as you did above.
Personally I prefer #2, but please note that if you try to pick 6 random elements from an array of fewer than 6 elements with array_rand()
you’ll get a warning. You’d want to test the size of $author_array
with count($author_array)
first to either limit your array_rand()
command to the max size of the potential list of authors, or to skip it entirely if it’s 1.
Something like, after your first foreach ends:
if(count($author_array) >= 6) {
$max = 6;
} else {
$max = count($author_array);
}
$random_authors = array_rand($author_array,$max);
foreach ($random_authors as $author) { ...