我认识到,还有其他几个问题——但是,这些问题要么对我有意义,要么不做我所需要的工作。
我有一个阵列,通过使用<代码>sort(>和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}]
使用<代码>sort(和arsort(>
>,按Name>
字母或正反字母顺序排列。 现在,我需要同样的功能,只能依据dealType>
。
I have tried a few examples already posted on Stackoverflow, but I must misunderstand them because they are not working. How might I accomplish this?
EDIT Jonathan gave the correct answer for alphabetical sorting, with a slight modification:
//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 );