【发布时间】:2019-11-08 16:55:06
【问题描述】:
尝试根据从 Invision powerboard API(论坛软件)获得的帖子计数对热门海报进行排序。不知道如何对我的 foreach 循环的这个回声进行排序。
$curl = curl_init( $communityUrl . '/core/members/' );
curl_setopt_array( $curl, array(
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => "{$apiKey}:"
) );
$response = curl_exec( $curl );
$data = json_decode($response, true);
$count = 0;
foreach($data as $member) {
if (is_array($member)) {
foreach($member as $name) {
if($count > 4)
return;
echo '<p class="top-member-p"><a href="'.$name['profileUrl'].'">'.ucfirst($name['name']).'</a> has '.$name['posts'] . ' posts</p>';
$count++;
}
}
}
我希望按照他们的帖子数对结果进行排序,如下所示:
"Swaghetti has 34 posts"<br>
"Josh has 15 posts"<br>
"Test has 3 posts"<br>
"Testuser2 has 0 posts"
但实际上是这样的:
"Swaghetti has 34 posts"<br>
"Testuser2 has 0 posts"<br>
"Test has 3 posts"<br>
"Josh has 15 posts"
【问题讨论】:
标签: php arrays sorting foreach