0

I have tried to use PHP decode to parse my JSON string below into an array so I can extract the current and day for both channels from the file.

my json file owl_output.json looks like..

{"channels":{"0":[{"current":1288,"units":"w"},{"day":31278.57,"units":"wh"}],"1":    [{"current":660,"units":"w"},{"day":9191.11,"units":"wh"}]}}

I'am only ever getting one result displayed, the php code I have managed to get working is below

<?php
$string = file_get_contents('owl_output.json');
$data = json_decode($string,true);
print_r($json);
foreach ($data['channels']['0'] as $data)
{
    echo $data ['current'];
}
?>

This only display the current for channel 0. If I try to add additional fields it doesn't display

echo $data ['current']['day']; ( doesn't work )

Can someone advise how I can display current and day for both channels 0 & 1 ?

My aim is to display this in a html page at the end and to keep polling the json file?

The array it displays is below

Array
(
    [channels] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [current] => 1288
                            [units] => w
                        )

                    [1] => Array
                        (
                            [day] => 31278.57
                            [units] => wh
                        )

                )

            [1] => Array
                (
                    [0] => Array
                        (
                            [current] => 660
                            [units] => w
                        )

                    [1] => Array
                        (
                            [day] => 9191.11
                            [units] => wh
                        )

                )

        )

)

Can anyone offer any assistance with this ?

Thanks

1
  • You should loop over $data['channels'], not $data['channels'][0]. Commented Oct 3, 2013 at 19:01

3 Answers 3

1

The variable $data is conflicting:

Used to store the data, and used in the foreach loop. Rename the $data variable in the foreach for example:

<?php
$string = file_get_contents('owl_output.json');
$data = json_decode($string,true);
print_r($json);
foreach ($data['channels'] as $channel)
{
    echo $channel[0]['current'];
    echo $channel[1]['day'];
}
?>

I did edit since there was an other error because there is not 'current' in every record.

Sign up to request clarification or add additional context in comments.

Comments

0

Conflict on $data reference in loop and bad array indexes :

foreach ($data['channels'] as $channel)
{
    echo $channel[0]['current'];
    echo $channel[1]['day'];
}

3 Comments

This has worked, however I would like to display all the results on a new line for each result. Currently this displays to the screen as one long line.
If you use html output, use tags for formating, as <br>, if output is in raw text, use "\n" to go to new line.
thanks again that has worked. If I wanted to poll the owl_output.js file. where should the call go in the code ? <?php $string = file_get_contents('test/owl_output.js'); $data = json_decode($string,true); print_r($json); foreach ($data['channels'] as $channel) { echo "Current " . $channel[0]['current']. "<br>"; echo "Day " . $channel[1]['day']. "<br>"; } setInterval(function() { $.getJSON("test/owl_output.js", function(data) { // update the view with your fresh data }); }, 5000); } ?>
0
foreach ($data['channels'] as $chanel)
{
    echo $chanel[0]['current'];
    echo $chanel[1]['day'];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.