array_multisort无效

时间:2014-08-22 09:23:54

标签: php arrays sorting

我有两个不同的数组

$ player_details

Array ( 
[83] => Array ( [number] => 2 
                [name] => Mario Goetze 
                [position] => Sturm
                [age] => 37 ) 
[96] => Array ( [number] => 4 
                [name] => Lukas Podolski 
                [position] => Torwart
                [age] => 24 ) 
[66] => Array ( [number] => 1 
                [name] => Marco Reuss 
                [position] => Langzeitverletzt
                [age] => 24 ) 
[359] => Array ( [number] => 99 
                 [name] => Inge Schmidt 
                 [position] => Mittelfeld
                 [age] => 23 )
 )

$ array_positions

$array_positions = array("Torwart", "Abwehr", "Mittelfeld", "Sturm", "Spielberechtigte Talente (A-Jugend)", "Langzeitverletzt");

我想在$ array_positions之后对$ player_details进行排序,以便为我的足球队获得正确的顺序

array_multisort($player_details["position"],$array_positions);

但那不起作用。什么可能是错的?

1 个答案:

答案 0 :(得分:0)

您尝试通过单独的排序算法对数组进行排序。看一下php函数usort

http://de2.php.net/manual/de/function.usort.php

usort($player, function($a, $b) use ($array_positions){
    $aPos = $a['position'];
    $aVal = array_search($aPos, $array_positions);
    $bPos = $b['position'];
    $bVal = array_search($bPos, $array_positions);

    if( $aVal == $bVal ) return 0;
    return ($aVal < $bVal) ? -1 : 1;
});