0

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>
AnnaLA
  • 155
  • 1
  • 11

1 Answers1

1

Here's how I would do it:

<?php
$data = json_decode(file_get_contents($url));

$json = []; $prev = null;
foreach ($data[0]->price as $item)
{
    while (isset($prev) && strtotime($item->date) != ($prev=strtotime('+1 day', $prev)))
        $json []= [date('Y-m-d', $prev), $item->value];

    $json []= [$item->date, $item->value];

    $prev = strtotime($item->date);
}
?>

<script type="text/javascript">window.profileData = <?=json_encode($json)?></script>

For this input:

$data = json_decode('[{"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}]}]');

It generates:

window.profileData = [["1982-11-12",0.5781],["1982-11-13",0.5513],["1982-11-14",0.5513],["1982-11-15",0.5513],["1982-11-16",0.5513],["1982-11-17",0.5513],["1982-11-18",0.5513],["1982-11-19",0.5513],["1982-11-20",0.5179],["1982-11-21",0.5179],["1982-11-22",0.5179],["1982-11-23",0.5179],["1982-11-24",0.5179],["1982-11-25",0.5179],["1982-11-26",0.5179],["1982-11-27",0.567],["1982-11-28",0.567],["1982-11-29",0.567],["1982-11-30",0.567],["1982-12-01",0.567],["1982-12-02",0.567],["1982-12-03",0.567],["1982-12-04",0.5223],["1982-12-05",0.5223],["1982-12-06",0.5223],["1982-12-07",0.5223],["1982-12-08",0.5223],["1982-12-09",0.5223],["1982-12-10",0.5223],["1982-12-11",0.5379],["1982-12-12",0.5379],["1982-12-13",0.5379],["1982-12-14",0.5379],["1982-12-15",0.5379],["1982-12-16",0.5379],["1982-12-17",0.5379],["1982-12-18",0.5714],["1982-12-19",0.5714],["1982-12-20",0.5714],["1982-12-21",0.5714],["1982-12-22",0.5714],["1982-12-23",0.5714],["1982-12-24",0.5714],["1982-12-25",0.5335],["1982-12-26",0.5335],["1982-12-27",0.5335],["1982-12-28",0.5335],["1982-12-29",0.5335],["1982-12-30",0.5335],["1982-12-31",0.5335],["1983-01-01",0.4911],["1983-01-02",0.4911],["1983-01-03",0.4911],["1983-01-04",0.4911],["1983-01-05",0.4911],["1983-01-06",0.4911],["1983-01-07",0.4911],["1983-01-08",0.5893],["1983-01-09",0.5893],["1983-01-10",0.5893],["1983-01-11",0.5893],["1983-01-12",0.5893],["1983-01-13",0.5893],["1983-01-14",0.5893],["1983-01-15",0.6674],["1983-01-16",0.6674],["1983-01-17",0.6674],["1983-01-18",0.6674],["1983-01-19",0.6674],["1983-01-20",0.6674],["1983-01-21",0.6674]]
Sjon
  • 4,989
  • 6
  • 28
  • 46