<?php
$data = [
[
'Jon',
'NO',
"",
"Private",
120
],
[
'Andew',
'NO',
"",
"VC",
150
],
[
'Walid',
'YES',
"",
"Other",
160
],
[
'Andew',
'YES',
"",
"VC",
150
],
[
'Andew',
'YES',
"",
"VC",
180
],
[
'Jon',
'NO',
"",
"Other",
150
],
[
'Andew',
'YES',
"",
"Other",
600
]
];
$arr = array();
foreach ($data as $key => $item) {
$arr[$item[0]][$item[3]] = ($arr[$item[0]][$item[3]] ?? 0) + $item[4];
}
foreach (array_unique(array_column($data, 3)) as $type) {
foreach ($arr as &$person) {
if (!isset($person[$type])) $person[$type] = 0;
}
}
uasort($arr, function ($a, $b) {
return $b['Private'] + $b['Other'] + $b['VC'] - ($a['Private'] + $a['Other'] + $a['VC']);
});
$key_rank = array('Private' => 0, 'VC' => 1, 'Other' => 2);
foreach ($arr as &$person) {
uksort($person, function ($a, $b) use ($key_rank) {
return $key_rank[$a] - $key_rank[$b];
});
$person = array_values($person);
}
print_r($arr);
- Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
- Array
(
[Andew] => Array
(
[0] => 0
[1] => 480
[2] => 600
)
[Jon] => Array
(
[0] => 120
[1] => 0
[2] => 150
)
[Walid] => Array
(
[0] => 0
[1] => 0
[2] => 160
)
)
- Output for 8.3.5
- Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
Array
(
[Andew] => Array
(
[0] => 0
[1] => 480
[2] => 600
)
[Jon] => Array
(
[0] => 120
[1] => 0
[2] => 150
)
[Walid] => Array
(
[0] => 0
[1] => 0
[2] => 160
)
)
preferences:
151.53 ms | 402 KiB | 178 Q