Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to parse a nested associative array in PHP to echo in a specific order?

New to working with JSON data in PHP, been stuck on this problem all day. I have a nested associative JSON array of OHLC/V values for a certain stock. Here is a minimal example of the JSON file as returned by json_decode() with the true flag.

$data = array(
    "c"=>array(
        0 => "162.75",
        1 => "162.27"
    ),
    "h"=>array(
        0 => "166.77",
        1 => "166.22"
    ),
    "l"=>array(
        0 => "162.5",
        1 => "162.2"
    ),
    "o"=>array(
        0 => "164.63",
        1 => "162.67"
    ),
    "s"=>"ok",
    "t"=>array(
        0 => "1679443200",
        1 => "1679529600"
    ),
    "v"=>array(
        0 => "4367267",
        1 => "1865365"
    )
);

For each letter-named array (besides s), I need to get a single line output in the following format:

array("x" => tVal, "y" => array(oVal ,hVal ,lVal ,cVal, vVal)),

Here is a sample of what the result for "0" should be:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

array("x" => 1679443200, "y" => array(164.63 ,166.77 ,162.5 ,162.75, 4367267)),

Each of these array() lines will be made into another array with the following format in order to create a candlestick chart.

$dataPoints = array( 

   array(result1),

   array(result2),
   array(resultN)

);

The real JSON data will return 90 days of data, so ideally the loop has to go up to 90 but I’d prefer it to automatically stop at the end of the array if there are less than 90 days available.

Here’s what I’ve come up with while testing out some possible solutions:

$i = 0;
while($i <= 90){
    foreach($data as $x => $val){
        if($x == 't'){
            echo 'array(x => '.$val[$i].'), y=array(';
            echo '<br>';
        }
        if($x != 's' and $x != 't'){
          echo $x.'='.$val[$i];
          echo '<br>';
        }
    }
    $i++;
    echo '<br>';
}

A sample output for row 0 is:

c=162.75
h=166.77
l=162.5
o=164.63
array(x => 1679443200), y=array(
v=4367267

As you can see, I am able to get the proper output array started and move the ‘t’ result into the proper place, but I was unable to figure out a way to properly order ‘o’,’h’,’l’,’c’, and ‘v’. Any help would be greatly appreciated!

>Solution :

You don’t need the nested loop. Just index the appropriate nested arrays.

$results = [];
for ($i = 0; $i <= 90; $i++) {
    $row = [
        'x' => $data['x'][$i], 
        'y' => [$data['o'][$i], $data['h'][$i], $data['c'][$i], $data['v'][$i]]
    ];
    $results[] = $row;
}
print_r($results);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading