简体   繁体   English

array_multisort不起作用

[英]array_multisort not working

I have two different arrays 我有两个不同的数组

$player_details $ 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_positions = array("Torwart", "Abwehr", "Mittelfeld", "Sturm", "Spielberechtigte Talente (A-Jugend)", "Langzeitverletzt");

I wanted to sort $player_details after $array_positions to get the right order for my footbal team 我想在$ array_positions之后对$ player_details进行排序,以便为我的足球队找到正确的顺序

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

But that's not working. 但这不起作用。 What can be wrong? 有什么事吗

you try to sort an array by an individually sort algo. 您尝试通过单独的排序算法对数组进行排序。 look at the php function usort 看一下PHP函数usort

http://de2.php.net/manual/de/function.usort.php 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;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM