Create array from another json array in php

Asked

Viewed 39 times

1

Guys, I have the following array coming from a url that I’m picking up via file_get_contents

$urlHistorical      = 'https://mfinance.com.br/api/v1/stocks/historicals/IBOV?months=3';
                                $dadosHistorical    = file_get_contents($urlHistorical);
                                $data_historical    = json_decode($dadosHistorical);
                                $historical         = $data_historical->historicals;

Here the return already filtered in historicals

array (size=59)
  0 => 
    object(stdClass)[2]
      public 'close' => float 103515.16
      public 'date' => string '2020-11-09T00:00:00Z' (length=20)
      public 'high' => float 105146.56
      public 'low' => float 100953.95
      public 'open' => float 100953.95
      public 'volume' => int 0
  1 => 
    object(stdClass)[3]
      public 'close' => float 105066.96
      public 'date' => string '2020-11-10T00:00:00Z' (length=20)
      public 'high' => float 105758.29
      public 'low' => float 103452.64
      public 'open' => float 103516.37
      public 'volume' => int 0
  2 => 
    object(stdClass)[4]
      public 'close' => float 104808.83
      public 'date' => string '2020-11-11T00:00:00Z' (length=20)
      public 'high' => float 105462.33
      public 'low' => float 104143.47
      public 'open' => float 105066.96
      public 'volume' => int 0

I’m needing to turn the above return into this for mounting a chart, can anyone give me a help how to do this in php? Thanks

The values I will use will be in x: 'date', in y: [high, low, open, close]

data: [{
                    x: new Date(1538778600000),
                    y: [6629.81, 6650.5, 6623.04, 6633.33]
                },
                {
                    x: new Date(1538780400000),
                    y: [6632.01, 6643.59, 6620, 6630.11]
                },
                {
                    x: new Date(1538782200000),
                    y: [6630.71, 6648.95, 6623.34, 6635.65]
                },

1 answer

1


I don’t know if this is the way you need it, but I hope it serves as a stepping stone to your project.

<?php  

    $urlHistorical = 'http://mfinance.com.br/api/v1/stocks/historicals/IBOV?months=3'; // url exemplo...
    
    // ilustração dos dados recebidos da API (em JSON codificado)
    $dadosHistorical = '{"historicals":[{"close":103515.16,"date":"2020-11-09T00:00:00Z","high":105146.56,"low":100953.95,"open":100953.95,"volume":0},{"close":105066.96,"date":"2020-11-10T00:00:00Z","high":105758.29,"low":103452.64,"open":103516.37,"volume":0},{"close":104808.83,"date":"2020-11-11T00:00:00Z","high":105462.33,"low":104143.47,"open":105066.96,"volume":0},{"close":102507.01,"date":"2020-11-12T00:00:00Z","high":105018.66,"low":102033.75,"open":104810.06,"volume":0},{"close":104723,"date":"2020-11-13T00:00:00Z","high":104725.79,"low":102508.77,"open":102508.77,"volume":0},{"close":106429.92,"date":"2020-11-16T00:00:00Z","high":106518.16,"low":104728.51,"open":104728.51,"volume":0},{"close":107248.63,"date":"2020-11-17T00:00:00Z","high":107810.31,"low":105846.62,"open":106430.04,"volume":0},{"close":106119.09,"date":"2020-11-18T00:00:00Z","high":107467.25,"low":106043.35,"open":107245.54,"volume":0},{"close":106669.9,"date":"2020-11-19T00:00:00Z","high":106972.88,"low":105545.37,"open":106117.26,"volume":0},{"close":106042.48,"date":"2020-11-20T00:00:00Z","high":106763.96,"low":105680.28,"open":106669.9,"volume":0},{"close":107378.92,"date":"2020-11-23T00:00:00Z","high":107495.35,"low":106050.48,"open":106050.48,"volume":0},{"close":109786.3,"date":"2020-11-24T00:00:00Z","high":109956.18,"low":107360.58,"open":107377.95,"volume":0},{"close":110132.53,"date":"2020-11-25T00:00:00Z","high":110595.81,"low":109315.21,"open":109786.37,"volume":0},{"close":110227.09,"date":"2020-11-26T00:00:00Z","high":110244.5,"low":109418.33,"open":110132.53,"volume":0},{"close":110575.47,"date":"2020-11-27T00:00:00Z","high":111603.41,"low":110161.1,"open":110228.62,"volume":0},{"close":108893.32,"date":"2020-11-30T00:00:00Z","high":110933.68,"low":108829.15,"open":110598.01,"volume":0},{"close":111399.91,"date":"2020-12-01T00:00:00Z","high":111839.86,"low":108897.29,"open":108897.29,"volume":0},{"close":111878.53,"date":"2020-12-02T00:00:00Z","high":112315.42,"low":110579.16,"open":111401.76,"volume":0},{"close":112291.59,"date":"2020-12-03T00:00:00Z","high":113377.33,"low":111873.88,"open":111873.88,"volume":0},{"close":113750.22,"date":"2020-12-04T00:00:00Z","high":113863.73,"low":112293.76,"open":112293.77,"volume":0},{"close":113589.77,"date":"2020-12-07T00:00:00Z","high":114531.04,"low":112629.18,"open":113750.5,"volume":0},{"close":113793.06,"date":"2020-12-08T00:00:00Z","high":114381.14,"low":112820.31,"open":113589.77,"volume":0},{"close":113001.16,"date":"2020-12-09T00:00:00Z","high":114020.4,"low":112566.73,"open":113798.53,"volume":0},{"close":115128.63,"date":"2020-12-10T00:00:00Z","high":115261.71,"low":112731.76,"open":113002.26,"volume":0},{"close":115128,"date":"2020-12-11T00:00:00Z","high":115526.57,"low":113949.39,"open":115127.74,"volume":0},{"close":114611.12,"date":"2020-12-14T00:00:00Z","high":115740.1,"low":114431,"open":115136.91,"volume":0},{"close":116148.63,"date":"2020-12-15T00:00:00Z","high":116389.82,"low":114613.42,"open":114613.42,"volume":0},{"close":117857.35,"date":"2020-12-16T00:00:00Z","high":118178.44,"low":115495.75,"open":116148.97,"volume":0},{"close":118400.57,"date":"2020-12-17T00:00:00Z","high":119027.05,"low":117856.26,"open":117863.58,"volume":0},{"close":118023.67,"date":"2020-12-18T00:00:00Z","high":119370.48,"low":117638.24,"open":118395.23,"volume":0},{"close":115822.57,"date":"2020-12-21T00:00:00Z","high":118020.94,"low":114730.05,"open":118020.94,"volume":0},{"close":116636.18,"date":"2020-12-22T00:00:00Z","high":116902.54,"low":115648.34,"open":115824.66,"volume":0},{"close":117806.85,"date":"2020-12-23T00:00:00Z","high":118311.44,"low":116636.18,"open":116636.18,"volume":0},{"close":119123.7,"date":"2020-12-28T00:00:00Z","high":119212.79,"low":117804.95,"open":117805.56,"volume":0},{"close":119409.15,"date":"2020-12-29T00:00:00Z","high":119860.91,"low":118750.1,"open":119130.06,"volume":0},{"close":119017.24,"date":"2020-12-30T00:00:00Z","high":120149.85,"low":118919.43,"open":119409.85,"volume":0},{"close":118854.71,"date":"2021-01-04T00:00:00Z","high":120353.81,"low":118061.77,"open":119024.29,"volume":0},{"close":119376.21,"date":"2021-01-05T00:00:00Z","high":119790.06,"low":116756.08,"open":118834.88,"volume":0},{"close":119100.08,"date":"2021-01-06T00:00:00Z","high":120924.32,"low":118916.94,"open":119376.91,"volume":0},{"close":122385.92,"date":"2021-01-07T00:00:00Z","high":122696.64,"low":119100.76,"open":119103.07,"volume":0},{"close":125076.63,"date":"2021-01-08T00:00:00Z","high":125323.53,"low":122385.76,"open":122387.43,"volume":0},{"close":123255.13,"date":"2021-01-11T00:00:00Z","high":125075.18,"low":122505.58,"open":125075.18,"volume":0},{"close":123998,"date":"2021-01-12T00:00:00Z","high":124584.33,"low":123227.47,"open":123255.13,"volume":0},{"close":121933.08,"date":"2021-01-13T00:00:00Z","high":124031.68,"low":121015.6,"open":123996.2,"volume":0},{"close":123480.52,"date":"2021-01-14T00:00:00Z","high":123896.35,"low":121946.67,"open":121946.67,"volume":0},{"close":120348.8,"date":"2021-01-15T00:00:00Z","high":123471.59,"low":120185.13,"open":123471.59,"volume":0},{"close":121241.63,"date":"2021-01-18T00:00:00Z","high":122585.82,"low":120351.43,"open":120351.43,"volume":0},{"close":120636.39,"date":"2021-01-19T00:00:00Z","high":122120.24,"low":119257.03,"open":121249.84,"volume":0},{"close":119646.4,"date":"2021-01-20T00:00:00Z","high":121449.1,"low":118739.87,"open":120644.5,"volume":0},{"close":118328.99,"date":"2021-01-21T00:00:00Z","high":120242.86,"low":117785.13,"open":119628.41,"volume":0},{"close":117380.49,"date":"2021-01-22T00:00:00Z","high":118317.52,"low":116108.9,"open":118317.52,"volume":0},{"close":116464.06,"date":"2021-01-26T00:00:00Z","high":119167.14,"low":116109.1,"open":117380.89,"volume":0},{"close":115882.3,"date":"2021-01-27T00:00:00Z","high":117839.79,"low":114886.51,"open":116463.45,"volume":0},{"close":118883.25,"date":"2021-01-28T00:00:00Z","high":119355.23,"low":115733.6,"open":115882.3,"volume":0},{"close":115067.55,"date":"2021-01-29T00:00:00Z","high":118879.9,"low":114973,"open":118879.9,"volume":0},{"close":117517.57,"date":"2021-02-01T00:00:00Z","high":117861.51,"low":115093.16,"open":115093.16,"volume":0},{"close":118233.81,"date":"2021-02-02T00:00:00Z","high":119805.18,"low":117519.17,"open":117520.36,"volume":0},{"close":119724.72,"date":"2021-02-03T00:00:00Z","high":120210.38,"low":118234.28,"open":118234.99,"volume":0},{"close":119260.82,"date":"2021-02-04T00:00:00Z","high":120559.98,"low":118917.65,"open":119727.75,"volume":0}],"symbol":"IBOV"}';

    $dataHistorical = json_decode($dadosHistorical, true); // decodifico o JSON como objeto..

    for ($i = 0; $i < sizeof($dataHistorical['historicals']); $i++) {
        $arrGraph['data'][] = array(
            'x' => $dataHistorical['historicals'][$i]['date'],
            'y' => array($dataHistorical['historicals'][$i]['high'],
                $dataHistorical['historicals'][$i]['low'],
                $dataHistorical['historicals'][$i]['open'],
                $dataHistorical['historicals'][$i]['close']
            )
        );
    }

    print_r(json_encode($arrGraph)); // resultado
  • Thanks champ, it worked right, I just needed to adjust the item [open] that has to be the first in the graphic assembly. I managed to do, more in a way that certainly would not be the most correct hehehe, its got much better, thank you

  • Dispose brother, define my answer as "accepted and functional" is below the Vows, next to the answer. So to be able to help other people, because your question is as Solved and Solved. Any doubts we are there!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.