Let us suppose I have an array like below
$item = Array(
[0] => Array(
"name" => "item1"
"score" => 100,
"category" => "A1",
"vote" => 80,
)
[1] => Array(
"name" => "item2",
"score" => 100,
"category" => "A1",
"vote" => 70,
)
[2] => Array(
"name" => "item3",
"score" => 80,
"category" => "A2",
"vote" => 80,
)
[3] => Array(
"name" => "item4",
"score" => 80,
"category" => "A2",
"vote" => 60,
)
[4] => Array(
"name" => "item5",
"score" => 80,
"category" => "A3",
"vote" => 80,
)
)
edited input:
Array
(
[0] => Array
(
[name] => item1
[score] => 100
[category] => A1
[vote] => 80
)
[1] => Array
(
[name] => item2
[score] => 100
[category] => A1
[vote] => 80
)
[2] => Array
(
[name] => item5
[score] => 80
[category] => A3
[vote] => 80
)
[3] => Array
(
[name] => item3
[score] => 80
[category] => A2
[vote] => 80
)
[4] => Array
(
[name] => item4
[score] => 80
[category] => A2
[vote] => 60
)
)
The priority of the sort is score
, category
and vote
consequently.
The expected result is :
$item = Array(
[0] => Array(
"name" => "item1"
"score" => 100,
"category" => "A1",
"vote" => 80,
)
[1] => Array(
"name" => "item5",
"score" => 80,
"category" => "A3",
"vote" => 80,
)
[2] => Array(
"name" => "item3",
"score" => 80,
"category" => "A2",
"vote" => 80,
)
[3] => Array(
"name" => "item2",
"score" => 100,
"category" => "A1",
"vote" => 70,
)
[4] => Array(
"name" => "item4",
"score" => 80,
"category" => "A2",
"vote" => 60,
)
)
edited expected result:
Array
(
[0] => Array
(
[name] => item1
[score] => 100
[category] => A1
[vote] => 80
)
[1] => Array
(
[name] => item5
[score] => 80
[category] => A3
[vote] => 80
)
[2] => Array
(
[name] => item3
[score] => 80
[category] => A2
[vote] => 80
)
[3] => Array
(
[name] => item2
[score] => 100
[category] => A1
[vote] => 80
)
[4] => Array
(
[name] => item4
[score] => 80
[category] => A2
[vote] => 60
)
)
I want to have the distinct categories at the top sorted based on the score
and then vote
but if they are two items with the same category, the item with the lower score will have lower priority.
Based on the insight from , I first sort the array based on score, category and vote
foreach($list as $k=>$v) {
$sorted['score'][$k] = $v['score'];
$sorted['category'][$k] = $v['category'];
$sorted['vote'][$k] = $v['vote'];
}
array_multisort($sorted['score'], SORT_DESC, $sorted['category'], SORT_DESC,$sorted['vote'], SORT_DESC, $list);
It will give me:
Array
(
[0] => Array
(
[name] => item1
[score] => 100
[category] => A1
[vote] => 80
)
[1] => Array
(
[name] => item2
[score] => 100
[category] => A1
[vote] => 80
)
[2] => Array
(
[name] => item5
[score] => 80
[category] => A3
[vote] => 80
)
[3] => Array
(
[name] => item3
[score] => 80
[category] => A2
[vote] => 80
)
[4] => Array
(
[name] => item4
[score] => 80
[category] => A2
[vote] => 60
)
)
I need to re-sort the result again to have the distinct categories at the top (see the edited expected result)
Edited: It seems my example and question are confusing. The idea is I want to sort the item based on the score and then vote but the category should be taken into consideration. On top of score and vote, I want to have the time with the distinct categories on top and then the score and vote follow.