I want to group array values and find total (sum of array value-based of the group by value) and then sort array based on total. I want to group by users by fund type ("Private, VC, Others") and the sum of total fund ("last value")
I have set up a demo link here.
<?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]][$key] = $item['4'];
}
var_dump($arr);
I want below output
Group by ("Private, VC, Others") so value format like [sum of the private, sum of VC, a sum of Others ]
Array
(
[Jon] => [120,110,0]
[Andew] => [0,480,600]
[Walid] => [0,0,160]
)
And then I want to sort array based on the total sum
Array
(
[Andew] => [0,480,600]
[Jon] => [120,110,0]
[Walid] => [0,0,160]
)
Anyone please suggest possible solution to fic this issue?
Thanks