1👍
✅
Try code below:
<?php
$data = array (
0 => array (
'name' => 'credit_card',
'revenue' => '150',
'date' => 'Apr 2020'
),
1 => array (
'name' => 'cash',
'revenue' => '180',
'date' => 'Apr 2020'
),
2 => array (
'name' => 'cash',
'revenue' => '80',
'date' => 'May 2020'
),
3 => array (
'name' => 'credit_card',
'revenue' => '60',
'date' => 'May 2020'
),
4 => array (
'name' => 'cash',
'revenue' => '160',
'date' => 'Jun 2020'
),
5 => array (
'name' => 'credit_card',
'revenue' => '300',
'date' => 'Jul 2020'
)
);
$result = array_reduce(
$data,
function($res, $d) {
if (!isset($res[$d['date']])) $res[$d['date']] = [
'date' => $d['date'],
'cash' => 0,
'credit_card' => 0
];
$res[$d['date']][$d['name']] = $d['revenue'];
return $res;
},
[]
);
var_export(array_values($result));