I have been searching this but did not find any solution anywhere. I want to re order my array with a specific index.
Like this:
$scoreboard = array(
'foo' => array(
'score' => 580,
'game' => 'google',
),
'bar' => array(
'score' => 1385,
'game' => 'facebook',
),
'car' => array(
'score' => 750,
'game' => 'tweet',
),
);
And the output should be like this:
$scoreboard = array(
'bar' => array(
'score' => 1385,
'game' => 'facebook',
),
'car' => array(
'score' => 750,
'game' => 'tweet',
),
'foo' => array(
'score' => 580,
'game' => 'google',
),
);
See that the array is rearranged with each item's 'score' index. Any help here?
Expanding on on my comment where I suggested to use the uasort function, here is a working sample: http://codepad.org/fBSqHNCC
Like mishu mentions in the comment, you can use the uasort
-function
Example:
$scoreboard = array(
'foo' => array(
'score' => 580,
'game' => 'google',
),
'bar' => array(
'score' => 1385,
'game' => 'facebook',
),
'car' => array(
'score' => 750,
'game' => 'tweet',
),
);
function arrayCompareScore( $a, $b ) {
if ( $a['score'] == $b['score'] ) {
return 0;
}
return ( $a['score'] > $b['score'] ) ? -1 : 1;
}
uasort( $scoreboard, 'arrayCompareScore' );
print_r( $scoreboard );