我意識到還有其他幾個與此有關的問題 - 但它們對我來說都沒有意義,或者根本無法工作。按特定值排序多維數組(使用asort和rasort)
我有一個數組,通過使用asort()
和arsort()
,可以按字母順序排列第一個值。我多維數組的一個例子可能是:
[{"Name":"Amber","date":"","dealType":"B-Braun Medical, Inc.","id":"***@***.com","registered":0},{"Name":"Bob","date":"","dealType":"B-Braun Medical, Inc.","id":"***@***.com","registered":0},{"Name":"Hans","date":"","dealType":"B-Braun Medical, Inc.","id":"***@***.com","registered":0},{"Name":"Jeff","date":"","dealType":"B-Braun Medical, Inc.","id":"***@***.com","registered":0},{"Name":"Michael","date":"","dealType":"B-Braun Medical, Inc.","id":"***@***.com","registered":0}]
隨着使用asort()
和arsort()
它是由"Name"
字母順序排序或反向字母。現在我需要相同的功能,僅基於"dealType"
。
我已經嘗試了幾個已經發布在Stackoverflow上的例子,但我必須誤解他們,因爲他們不工作。我該如何做到這一點?
編輯 喬納森給出的字母排序正確的答案,有輕微的修改:
//the custom function to do our sort
function cmp($a,$b){
//get which string is less or 0 if both are the same
$cmp = strcasecmp($a->dealType, $b->dealType);
//if the strings are the same, check name
return $cmp;
}
//sort using a custom function
usort($obj, 'cmp');
和下面這段代碼反向字母排序:
//the custom function to do our sort
function cmp($a,$b){
//get which string is less or 0 if both are the same
$cmp = strcasecmp($b->dealType, $a->dealType);
//if the strings are the same, check name
return $cmp;
}
//sort using a custom function
usort($obj, 'cmp');
它是通過.NET WCF給我的,否則我會做你編輯說的。你上面的代碼給出了某種錯誤。 – mdance 2012-04-19 21:14:11
舊版本的php(
2012-04-19 21:20:02
我有版本5.3.3 – mdance 2012-04-19 21:22:35