我得到了一个这样的数组
Array
(
[0] => Array
(
[UnitESN] => 14296
[ScanNo] => 1
[ScanDate] => Nov 21
[ScanTime] => 10:15 AM
[Qualifiers] =>
[Notes] =>
[BadgeData] => Array
(
[0] => HEATH---
[1] => MCCU----
[2] => HER---
[3] => HCONNORS@------
[4] =>
[5] => 393
[6] => 13350
[7] =>
[8] =>
[9] => 111
)
[Signal] => +00/000
[ConnectionDelay] => 0407
)
[1] => Array以此类推。我想订购ASC或DESC...假设第8列和第8列是BadgeData中的条目编号7 (8-1,因为它从零开始),有什么想法吗?我试过array_multisort,但没有成功。
谢谢
发布于 2012-03-27 01:16:35
我很高兴你想明白了。这是我在被打断之前开始写的东西。
Basic 示例:
<?php
function cmp($a, $b) {
if ($a['BadgeData'][7] == $b['BadgeData'][7]) {
return 0;
}
// Ascending
return ($a['BadgeData'][7] < $b['BadgeData'][7]) ? -1 : 1;
}
// Order the values of the array based on the value in BadgeData[7] in ascending order..
uasort($array, 'cmp');看起来我可能误解了您最初的问题,因为我以为您希望按BadgeData[7]中的值对数组进行排序,但似乎您希望对每个数组值的BadgeData进行排序。
发布于 2012-03-27 01:10:04
感谢@Francois Deschenes引导我找到正确的答案。这是我发现的:
http://ca2.php.net/manual/en/function.uasort.php#104714
我根据自己的需要进行了编辑。谢谢!
function SortArrayByCol(array $Array, $Key, $ASC=true, $Col=0) {
$Result = array();
$Values = array();
foreach($Array as $ID => $Value){
$Values[$ID] = isset($Value[$Key][$Col]) ? $Value[$Key][$Col] : null;
}
if($ASC){
asort($Values);
} else {
arsort($Values);
}
foreach($Values as $Key => $Value) {
$Result[$Key] = $Array[$Key];
}
return $Result;
}https://stackoverflow.com/questions/9876435
复制相似问题