>我有一个看起来像这样的表格:
_____________________________________________
NAME | VIEWS | COMMENTS| SUBSCRIBERS | VIDEOS
---------------------------------------------
X | 10 | 5 | 20 | 5
---------------------------------------------
Y | 50 | 10 | 10 | 5
---------------------------------------------
我的问题是,我将如何将通道 Y 放在表的顶部,因为我将此表排序为具有最多视图的通道,我没有使用 MySQL。这是我的代码:
@foreach($data["statistics"]["members"] as $key =>$member)
@foreach($member["channels"] as $channel)
<tr>
<td>{= $channel["snippet"]["title"] =}</td>
<td>{= $channel["statistics"]["viewCount"] =}</td>
<td>{= $channel["statistics"]["commentCount"] =}</td>
<td>{= $channel["statistics"]["subscriberCount"] =}</td>
<td>{= $channel["statistics"]["videoCount"] =}</td>
</tr>
@endforeach
@endforeach
如果你有PHP>5.3(因为你正在运行Laravel,你应该已经有了它),下面应该可以工作。
$channels = $member['channels'];
uasort($channels, function($a, $b) {
if( $a['statistics']['viewCount'] == $b['statistics']['viewCount'])
return 0;
return $a['statistics']['viewCount'] > $b['statistics']['viewCount']) ? -1 : 1;
});
foreach( $channels as $channel ) {
// rest of your code goes here
}
希望这有帮助!
在OOP中的php uasort