I'm trying to count integer value from JSON: https://pomber.github.io/covid19/timeseries.json but I got 1 for each key. What I expect is to count the total 'confirmed' from all countries by date as a key.
Here's my controller:
$client = new Client();$request = $client->get('https://pomber.github.io/covid19/timeseries.json');
$response = $request->getBody()->getContents();
$posts_dates = json_decode($response, true);
$confirmed_array = array();
if ( ! empty( $posts_dates ) ) {
foreach ( $posts_dates as $key => $val ) {
foreach ( ((array)$posts_dates)[$key] as $data ) {
$date_confirmed = new \DateTime( $data['date'] );
$day = $date_confirmed->format( 'd M y' );
$confirmed = count((array)$data['confirmed']);
$confirmed_array [ $day ] = $confirmed;
}
}
}
return $confirmed_array;
Here's the result:
{
"22 Jan 20": 1,
"23 Jan 20": 1,
"24 Jan 20": 1,
"25 Jan 20": 1,
"26 Jan 20": 1,
"27 Jan 20": 1,
"28 Jan 20": 1,
"29 Jan 20": 1,
"30 Jan 20": 1,
"31 Jan 20": 1,
....
}
UPDATE
I want to get the output to looks like below:
{
"date": [
"22 Jan 20",
"23 Jan 20",
"24 Jan 20",
"25 Jan 20",
.....
],
"total_confirmed": [
555,
653,
941,
1434,
.....
],
"max_value_of_total_confirmed": 12214
}
Any help would be appreciated :)