これに関して他にもいくつかの質問があることは承知していますが、それらは私には意味をなさないか、必要に応じて機能しません。
と を使用してアルファベット順に最初の値で簡単にソートできる配列が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');