I have a JSON file which looks like this:
[{
"num": "37"
}, {
"num": "79"
}, {
"num": "21"
}, {
"num": "12"
}, {
"num": "90"
}]
I need my script to print the sum of these numbers, here is what I tried:
<?php
$dati = file_get_contents('data.json');
$elementi = json_decode($dati, true);
$test = 'test';
$sum = 0;
foreach($elementi['num'] as $key=>$value)
{
$sum+= $value;
}
?>
But I got the error:
Notice: Undefined index: num in C:\xampp\htdocs\index.php on line 32
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\index.php on line 32
I'm stuck here and I can't find a solution. Can someone please help me?