25Hi我已經檢查了這裏,並不能找到一個類似的結果,因此,如果這已問我道歉。從高分到低分排序PHP多維數組
無論如何,我產生已由一個不錯的類生成的多陣列。
反正數組就這樣產生了,當輸出:
array(2047) {
[0]=> array(6) {
["name"]=> string(4) "name"
["gamesplayed"] => string(2) "26"
["points"] => string(2) "26"
}
[1]=> array(6) {
["name"]=> string(4) "name"
["gamesplayed"] => string(2) "26"
["points"] => string(2) "42"
}
[2]=> array(6) {
["name"]=> string(4) "name"
["gamesplayed"] => string(2) "26"
["points"] => string(2) "16"
}
// and so on
}
我的問題是...有一個簡單的PHP函數或使用點值的數組排序爲前高後低簡潔的方式?
我有看一些PHP函數,但它們只是用一個值標準芯片。
感謝您的幫助給:)
UPDATE
我試圖這樣做,它由點不排序。
這裏是我用做它的類:
//create sort value
private function sorting_table($a, $b){
return $a['points'] - $b['points'];
}
//sort all users into a table and order the highest to the lowest
public function output_prediction_table($pdo, $year){
//get all uers and put in an array with games played and points.
$getallusers = $pdo->prepare("SELECT userid FROM userpredictions WHERE year=:year");
$getallusers->execute(array(':year' => $year));
//create the multidimentional array
$multidimentional_array = array();
while($fetchallusers = $getallusers->fetch()){
$multidimentional_array[] = array(
'name' => $this->get_username($pdo, $fetchallusers['userid']),
'games' => $this->get_games_played($pdo, $year),
'points' => $this->make_points($pdo, $year, $fetchallusers['userid']),
'userid' => $fetchallusers['userid']
);
}
usort($multidimentional_array, array($this, 'sorting_table'));
$output = '<table class="table-striped">';
$output .= '<tr class="t-h"><td class="t-c">Position</td><td>Username</td><td>Games Played</td><td>Points</td></tr>';
$tablenumber = 1;
foreach($multidimentional_array as $newarray){
$output .= '<tr><td class="t-c">'.$tablenumber.'</td><td><a href="/user/'.$this->sluggify($newarray["name"]).'/'.$newarray["userid"].'/" title="View '.ucwords($newarray["name"]).' Profile">'.ucwords($newarray["name"]).'</a></td><td>'.$newarray["games"].'</td><td>'.$newarray["points"].'</td></tr>';
$tablenumber++;
}
$output .= '</table>';
return $output;
}
我沒有得到任何錯誤,它只是不具有最高點排序第一去年的最低點。
你見過[按Valu排序多維數組E](http://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value) –
@SatishSharma提到我已經看了,但並沒有看到這一點,虐待看看,看看它的工作原理。由於 – Robert