массивы — сортировка элементов по частоте в переполнении стека

Входные данные:

$result = Array
(
Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "y",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "x",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "y",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "y",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "y",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "x",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "y",
),

Array
(
"name" => "ლუკა ქურასბედიანი",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "z",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "z",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "x",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "z",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "x",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "x",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "x",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "x",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "x",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "x",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "x",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "x",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "z",
),
Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "y",
),
Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "y",
),
Array(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "z",
),

);

Я хочу отсортировать элементы по частоте (по «parrent_club_hash_id»).

Есть 11-x 7-y а также 5-z так что я хочу вывод, как это:

$result = Array
(
Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "x",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "x",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "x",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "x",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "x",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "x",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "x",
),

Array
(
"name" => "ლუკა ქურასბედიანი",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "x",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "x",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "x",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "x",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "y",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "y",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "y",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "y",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "y",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "y",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "y",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "z",
),

Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "z",
),
Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "z",
),
Array
(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "z",
),
Array(
"name" => "",
"squad_member_hash_id" => "",
"parrent_club_hash_id" => "z",
),

);

1

Решение

Сначала получите частоты ваших значений:

$values = array_count_values(array_column($result, 'parrent_club_hash_id'));

Затем сортируйте по этим частотам, используя пользовательскую сортировку:

usort($result, function ($x, $y) use ($values) {
// $y is first because you need descending sort
return $values[$y["parrent_club_hash_id"]] - $values[$x["parrent_club_hash_id"]];
});

Пример: http://sandbox.onlinephpfunctions.com/code/6265192e677641afbc21e899d97f252c14b5402f

0

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]