Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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

share|improve this question
    
You should loop over $data['channels'], not $data['channels'][0]. – The Alpha Oct 3 '13 at 19:01
up vote 0 down vote accepted

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

foreach ($data['channels'] as $channel)
{
    echo $channel[0]['current'];
    echo $channel[1]['day'];
}
share|improve this answer
    
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. – user2843830 Oct 8 '13 at 16:48
    
If you use html output, use tags for formating, as <br>, if output is in raw text, use "\n" to go to new line. – Koryonik Oct 9 '13 at 5:22
    
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); } ?> – user2843830 Oct 9 '13 at 17:00

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.

share|improve this answer
foreach ($data['channels'] as $chanel)
{
    echo $chanel[0]['current'];
    echo $chanel[1]['day'];
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.