我意识到还有其他几个与此有关的问题 - 但它们对我来说都没有意义,或者根本无法工作。按特定值排序多维数组(使用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