I'm creating a simple script in php and json, however I have some difficulties to make Sums of multi values by date. I want Sum "Import" and "Export" from each month.
This is what I want to print:
array (
'Date' => '2019-03',
'Import' => 1000,
'Export' => 250,
)
array (
'Date' => '2019-04',
'Import' => 100,
'Export' => 600,
)
My json:
[
{
"Date": "2019-03",
"Import": "200",
"Export": "50"
},
{
"Date": "2019-03",
"Import": "800",
"Export": "200"
},
{
"Date": "2019-04",
"Import": "100",
"Export": "600"
}
]
This is my script php:
$url = dirname(__DIR__ ) . '/admin/json/all.json';
$json = file_get_contents($url);
$array_origin = json_decode($json, TRUE);
$stack=array();
foreach ($array_origin as $index => $array_part) {
$stack[$array_part['Date']] =array_key_exists($array_part['Date'],$stack)?$stack[$array_part['Date']]+$array_part['Import']:$array_part['Import'];
}
echo '<pre>' . var_export($stack, true) . '</pre>';
However with this script Im only able to Sum 'Import', but I want also Sum 'export'.
For that reason Im looking for your help. I appreciate for any help from you guys.
Thanks in advance. Cheers