您应该编写一个函数来过滤数组,然后您可以像这样弹出最后一项:
// use this section of code in PHP >= 5.3 - utilizes anonymous function
$filtered_array = array_filter($authors, function($author) {
if(count_user_posts($author->id) >= 1) return true;
return false;
});
// use this section of code for older versions of PHP - uses regular function
function author_filter($author) {
if(count_user_posts($author->id) >= 1) return true;
return false;
}
$filtered_array = array_filter($authors, 'author_filter');
// the rest is the same regardless of PHP version
$last_author = array_pop($filtered_array);
// output
foreach($filtered_array as $author) {
// regular output
}
// then do special output for $last_author