我有一个如下数组,我想按键“attack”的值对该数组进行排序。数组的第一个键
(15, 13, 18)
是数据库中某些项目的ID,所以我不希望在数组排序时更改这些键。任何帮助将不胜感激。
这是数组:
$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'
)
);
uasort()
:
此函数使用用户定义的比较函数对数组进行排序,以便数组索引保持与其关联的数组元素的相关性。
这主要用于对实际元素顺序很重要的关联数组进行排序时使用。
示例:
function cmp($a, $b) {
if ($a['attack'] == $b['attack']) {
return 0;
}
return ($a['attack'] < $b['attack']) ? -1 : 1;
}
uasort($data, 'cmp');
如果值始终为字符串,您还可以在 strcmp()
函数中使用
cmp()
:
function cmp($a, $b) {
return strcmp($a['attack'], $b['attack']);
}
更新:
要按降序排序,您只需更改返回值:
return ($a['attack'] < $b['attack']) ? 1 : -1;
// ^----^
或接受@salathe 的建议:
return $b['attack'] - $a['attack'];
只需使用array_multisort
foreach ($data as $key => $row) {
$attack[$key] = $row['attack'];
}
// Sort the data with attack descending
array_multisort($attack, SORT_DESC, $data);
更新:
当数组键为字符串时,array_multisort
有效,但对于数字数组键,array_multisort
会破坏数组的第一级键。
保留第一级数组键
<?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);
?>
输出:
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
)
)
第一级数组键不保留
<?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);
?>
输出:
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
键可以是整数或字符串。该值可以是任何 类型。
此外,还会发生以下关键转换:
包含有效十进制整数的字符串,除非前面有数字 通过 + 号,将被强制转换为 int 类型。例如。键“8”将 实际上存储在 8 以下。另一方面,“08”不会被投射, 因为它不是有效的十进制整数。
浮点数也被转换为整数,这意味着小数部分 将被截断。例如。密钥 8.7 实际上将存储在 8 下。
布尔值也被转换为整数,即键 true 将实际被存储 在 1 下,密钥在 0 下为 false。
Null 将被转换为空字符串,即键 null 实际上将 存储在“”下。
数组和对象不能用作键。这样做会导致 警告:非法偏移类型。
$result = [];
foreach ($data as $key => $value) {
$result[$key] = $value;
asort($result[$key]);
}
print_r($result);