Simply use array_multisort
foreach ($data as $key => $row) {
$attack[$key] = $row['attack'];
}
// Sort the data with attack descending
array_multisort($attack, SORT_DESC, $data);
Update:
array_multisort
works when the array keys are string but in the case of numeric array keys, the array_multisort
destroys the first level keys of the array.
First level array keys are preserved
<?php
$data = array(
'a' => array(
'attack' => '45', 'defence' => '15', 'total' => '10'
),
'b' => array(
'attack' => '25', 'defence' => '15', 'total' => '10'
),
'c' => array(
'attack' => '35', 'defence' => '15', 'total' => '10'
)
);
print_r($data);
foreach ($data as $key => $row) {
$key = (string)$key;
$attack[$key] = $row['attack'];
}
print_r($attack);
// Sort the data with attack descending
array_multisort($attack, SORT_DESC, $data);
print_r($data);
?>
OUTPUT:
Array
(
[a] => Array
(
[attack] => 45
[defence] => 15
[total] => 10
)
[b] => Array
(
[attack] => 25
[defence] => 15
[total] => 10
)
[c] => Array
(
[attack] => 35
[defence] => 15
[total] => 10
)
)
Array
(
[a] => 45
[b] => 25
[c] => 35
)
Array
(
[a] => Array
(
[attack] => 45
[defence] => 15
[total] => 10
)
[c] => Array
(
[attack] => 35
[defence] => 15
[total] => 10
)
[b] => Array
(
[attack] => 25
[defence] => 15
[total] => 10
)
)
First level array keys are NOT preserved
<?php
$data = array(
'15' => array(
'attack' => '45', 'defence' => '15', 'total' => '10'
),
'13' => array(
'attack' => '25', 'defence' => '15', 'total' => '10'
),
'18' => array(
'attack' => '35', 'defence' => '15', 'total' => '10'
)
);
print_r($data);
foreach ($data as $key => $row) {
$key = (string)$key;
$attack[$key] = $row['attack'];
}
print_r($attack);
// Sort the data with attack descending
array_multisort($attack, SORT_DESC, $data);
print_r($data);
?>
OUTPUT:
Array
(
[15] => Array
(
[attack] => 45
[defence] => 15
[total] => 10
)
[13] => Array
(
[attack] => 25
[defence] => 15
[total] => 10
)
[18] => Array
(
[attack] => 35
[defence] => 15
[total] => 10
)
)
Array
(
[15] => 45
[13] => 25
[18] => 35
)
Array
(
[0] => Array
(
[attack] => 45
[defence] => 15
[total] => 10
)
[1] => Array
(
[attack] => 35
[defence] => 15
[total] => 10
)
[2] => Array
(
[attack] => 25
[defence] => 15
[total] => 10
)
)
https://www.php.net/manual/en/language.types.array.php
The key can either be an int or a string. The value can be of any
type.
Additionally, the following key casts will occur:
Strings containing valid decimal ints, unless the number is preceded
by a + sign, will be cast to the int type. E.g. the key "8" will
actually be stored under 8. On the other hand "08" will not be cast,
as it isn't a valid decimal integer.
Floats are also cast to ints, which means that the fractional part
will be truncated. E.g. the key 8.7 will actually be stored under 8.
Bools are cast to ints, too, i.e. the key true will actually be stored
under 1 and the key false under 0.
Null will be cast to the empty string, i.e. the key null will actually
be stored under "".
Arrays and objects can not be used as keys. Doing so will result in a
warning: Illegal offset type.