2014-07-17 49 views
0

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; 

} 

我沒有得到任何錯誤,它只是不具有最高點排序第一去年的最低點。

+0

你見過[按Valu排序多維數組E](http://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value) –

+0

@SatishSharma提到我已經看了,但並沒有看到這一點,虐待看看,看看它的工作原理。由於 – Robert

回答

1

嘗試usort

你可以做這樣的事情:

usort($array, 'sortByPoints'); 

function sortByOrder($a, $b) { 
    return $a['points'] - $b['points']; 
} 

喲可以在這裏找到更多的例子:Sort Multi-dimensional Array by Value

+1

你說得對,我是編輯:) – Sal00m

+0

多虧我帶進去一看usort :) – Robert

+0

伊夫更新我的問題,這並不工作:( – Robert

0

使用usort下面是完整的,經過測試的代碼:

function sortByOrder($a, $b) { 
    return $a['points'] - $b['points']; 
} 

$test = array(
    0=> array(
     "name"=> "name", 
     "gamesplayed" => "26", 
     "points" => "26" 
    ), 
    1=> array(
     "name"=> "name", 
     "gamesplayed" => "26", 
     "points" => "42", 
    ), 
    2=> array(
     "name"=> "name", 
     "gamesplayed" => "26", 
     "points" => "16", 
    ) 
); 

usort($test, 'sortByOrder'); 
print_r($test); 
+0

我已經更新了我的問題,以顯示我做了什麼,它不與你答曰工作,也許我有實施不正確 – Robert