0

I have two different arrays

$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");

I wanted to sort $player_details after $array_positions to get the right order for my footbal team

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

But that's not working. What can be wrong?

zerox76
  • 19
  • 3
  • Can you share your current output? – mTorres Aug 22 '14 at 09:30
  • 1
    `$player_details["position"]` does not exist, if anything it's `$player_details[..]["position"]`. Further, the contents of the `$array_positions` array and the positions in the `$player_details` array don't correspond at all (no, PHP doesn't translate from German to English automatically). In other words, you're just randomly throwing things together here which won't magically just work. That's what's wrong. Maybe read this? http://stackoverflow.com/q/17364127/476 – deceze Aug 22 '14 at 09:32
  • Sorry I changed the positions to their correct name. I saw this article but I can't take out anything constructive for me, it's just really to much for me. Can you maybe just give an advice how I can connect both arrays? – zerox76 Aug 22 '14 at 09:53

1 Answers1

0

you try to sort an array by an individually sort algo. look at the php function 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;
});
ins0
  • 3,918
  • 1
  • 20
  • 28