我意识到还有其他几个与此有关的问题 - 但它们对我来说没有意义,或者不按我的需要工作。
我有一个数组,很容易通过使用asort()
and来按字母顺序对第一个值进行排序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');