1๐
โ
$movements = [
[ 'amount' => 100, 'type' => 'expense', 'Dates' => '2020-01-01' ],
[ 'amount' => 100, 'type' => 'income', 'Dates' => '2020-01-01' ],
[ 'amount' => 200, 'type' => 'expense', 'Dates' => '2020-02-01' ],
[ 'amount' => 200, 'type' => 'income', 'Dates' => '2020-02-01' ],
[ 'amount' => 300, 'type' => 'income', 'Dates' => '2020-03-01' ],
[ 'amount' => 400, 'type' => 'expense', 'Dates' => '2020-04-01' ],
[ 'amount' => 400, 'type' => 'income', 'Dates' => '2020-04-01' ],
];
$dates = array_values(array_unique(array_column($movements, 'Dates')));
$income = [];
$expense = [];
foreach ($dates as $date) {
$item = array_values(array_filter($movements, fn($item) => $item['Dates'] === $date));
$amount1 = $item[0]['amount'];
$amount2 = count($item) === 2 ? $item[1]['amount'] : 0;
$expense[] = $item[0] === 'expense' ? $amount1 : $amount2;
$income[] = $item[0] === 'expense' ? $amount2 : $amount1;
}
print_r($dates);
print_r($income);
print_r($expense);
This will print:
Array
(
[0] => 2020-01-01
[1] => 2020-02-01
[2] => 2020-03-01
[3] => 2020-04-01
)
Array
(
[0] => 100
[1] => 200
[2] => 300
[3] => 400
)
Array
(
[0] => 100
[1] => 200
[2] => 0
[3] => 400
)