kurtko
kurtko

Reputation: 2126

PHP usort two arrays: first by value, second by key based on first array order

I have a multi-array like this:

Array
(
    ['player1'] => Array (
        ['a'] => 112
        ['b'] => 234
        ['c'] => 511
        ['d'] => 323
        ['e'] => 456
        ['f'] => 324
    )
    ['player2'] => Array (
        ['a'] => 545
        ['b'] => 434
        ['c'] => 158
        ['d'] => 053
        ['e'] => 982
        ['f'] => 111        
    )
)

I need to sort player1 by his value, then sort player2 with same key order.

I need this:

Array
(
    ['player1'] => Array (
        ['a'] => 112
        ['b'] => 234        
        ['d'] => 323
        ['f'] => 324
        ['e'] => 456
        ['c'] => 511        
    )
    ['player2'] => Array (
        ['a'] => 545
        ['b'] => 434
        ['d'] => 053
        ['f'] => 111        
        ['e'] => 982
        ['c'] => 158

    )
)

You can see how player1 is ordered by his values, then player2 is ordered by player1 keys order.

I have this code to order player1

usort ($my_array, 'sortByOrder'); 

function sortByOrder($a, $b) {    
    if ($a['player1'] < $b['player1'])
        return 1;    
}

Then I get this result:

Array
(
    ['player1'] => Array (
        ['0'] => 112
        ['1'] => 234        
        ['2'] => 323
        ['3'] => 324
        ['4'] => 456
        ['5'] => 511        
    )
    ['player2'] => Array (
        ['a'] => 545
        ['b'] => 434
        ['c'] => 158
        ['d'] => 053
        ['e'] => 982
        ['f'] => 111        
    )
)

I loose player1 keys, so I can't make a loop to order player2as player1 keys.

Any ideas?

Upvotes: 0

Views: 361

Answers (2)

B. Desai
B. Desai

Reputation: 16446

try this as per your need:

$my_array= array
(
    'player1' => array (
        'a' => 112,
        'b' => 234,
        'c' => 511,
        'd' => 323,
        'e' => 456,
        'f' => 324,
    ),
    'player2' => array (
        'a' => 545,
        'b' => 434,
        'c' => 158,
        'd' => 53,
        'e' => 982,
        'f' => 111,        
    )
);

asort($my_array['player1']);

$tmp_arr = $my_array['player2'];

$my_array['player2']=array();

foreach ($my_array['player1'] as $key => $value) {
    $my_array['player2'][$key]=$tmp_arr[$key];
}

var_dump($my_array);

Upvotes: 1

ha_ryu
ha_ryu

Reputation: 598

do you have to sort it by one function? if not this will work.

$a = Array ('player1' => Array (
                         'a' => 112,
                         'b' => 234,
                         'c' => 511,
                         'd' => 323,
                         'e' => 456,
                         'f' => 324
                        ), 
        'player2' => Array (
                        'a' => 545,
                        'b' => 434,
                        'c' => 158,
                        'd' => 053,
                        'e' => 982,
                        'f' => 111       
                        )   
);

asort($a['player1']);

foreach ($a['player1'] as $value) {
    echo $value."<br>";
}

echo "<hr>";

foreach ($a['player2'] as $value) {
    echo $value."<br>";
}

Upvotes: 0

Related Questions