I want to build a day-to-day date loop that relies on a JSON file. The loop should be adjusted to the graph. The problem that the dates in the JSON file are not necessarily sequential (depending on whether it's a business day or not) I want to view every day (regardless of whether it's a business day or not) which means I want to complete the JSON loop on dates.
For illustration, I will present an example. in case the json file will show me that
["1982-11-12", 0.4193],["1982-11-20", 0.3672],
i would like to display that into my graph
["1982-11-12", 0.4193],["1982-11-13", 0.4193],["1982-11-14", 0.4193],["1982-11-15", 0.4193],["1982-11-16", 0.4193],["1982-11-17", 0.4193],["1982-11-18", 0.4193],["1982-11-19", 0.4193],["1982-11-20", 0.3672]
In summary, I want the loop to continue to a consecutive date if the date is not. In addition to giving the same value of the previous date. TIA
Json file:
[
{ "id":"XXX","dateTimeRanges":{},
"price":[
{"date":"1982-11-12", "value":0.5781},
{"date":"1982-11-19","value":0.5513},
{"date":"1982-11-26","value":0.5179},
{"date":"1982-12-03","value":0.567},
{"date":"1982-12-10","value":0.5223},
{"date":"1982-12-17","value":0.5379},
{"date":"1982-12-24","value":0.5714},
{"date":"1982-12-31","value":0.5335},
{"date":"1983-01-07","value":0.4911},
{"date":"1983-01-14","value":0.5893},
{"date":"1983-01-21","value":0.6674}
]}]
Php file:
$json = file_get_contents($url);
//$json = json_encode($json, true);
$json_a = json_decode($json, true);
//print_r($json_a);
$n = count($json_a[0]['price']);
?>
<script type="text/javascript">
window.profileData = {
"stats": [
<?php
for($i=0;$i<$n;$i++){
$date = trim($json_a[0]['price'][$i]['date']);
$value = trim($json_a[0]['price'][$i]['value']);
$tomorrow = date("Y-m-d", strtotime('+1 day', strtotime($date)));
if($i > 0 and trim($json_a[0]['price'][$i+1]['date']) !== $tomorrow){
?>
["<?php echo $tomorrow;?>", <?php echo $value;?>],
<?php }else{ ?>
["<?php echo $date;?>", <?php echo $value;?>],
<?php }}?>],
"profile": {
"commonName": "<?php echo trim($name);?>",
}
};
</script>