0

I have fetched the JSON data using the following code.

$json = file_get_contents('http://api.openweathermap.org/data/2.5/weather?lat=35&lon=139'); 
$data = json_decode($json,true);

print_r($data);
foreach($data as $key => $datas){
$datas[$key];
   } 

this is the decoded json

   Array(
[coord] => Array
    (
        [lon] => 139
        [lat] => 35
    )

[sys] => Array
    (
        [message] => 0.0053
        [country] => JP
        [sunrise] => 1394571511
        [sunset] => 1394614136
    )

[weather] => Array
    (
        [0] => Array
            (
                [id] => 802
                [main] => Clouds
                [description] => scattered clouds
                [icon] => 03d
            )

    )

[base] => cmc stations
[main] => Array
    (
        [temp] => 289.82
        [pressure] => 1013
        [temp_min] => 289.82
        [temp_max] => 289.82
        [humidity] => 30
    )

[wind] => Array
    (
        [speed] => 3.08
        [gust] => 8.74
        [deg] => 230
    )

[clouds] => Array
    (
        [all] => 48
    )

[dt] => 1394611401
[id] => 1851632
[name] => Shuzenji
[cod] => 200
)

Then I tried to display the data using the Foreach method, but I couldn't got it right.

Can someone please help me with this.......

6
  • Please give us some more Information. What did you expect and what happened? Commented Mar 12, 2014 at 8:28
  • please provide your foreach code as well.. Commented Mar 12, 2014 at 8:28
  • what does $json show? Commented Mar 12, 2014 at 8:29
  • What is not right, it's an array and you could use foreach. Commented Mar 12, 2014 at 8:29
  • can you show text when you print array? Commented Mar 12, 2014 at 8:29

3 Answers 3

2

You can easily use the following examples to read your PHP array:

echo $data['coord']['lon'];
echo $data['coord']['lat'];
echo $data['sys']['message'];
echo $data['sys']['country'];
echo $data['sys']['sunrise'];
echo $data['sys']['sunset'];
.... And so on

Currently you receive:

Array
(
    [coord] => Array
        (
            [lon] => 139
            [lat] => 35
        )
[sys] => Array
    (
        [message] => 0.0422
        [country] => JP
        [sunrise] => 1394571511
        [sunset] => 1394614136
    )

[weather] => Array
    (
        [0] => Array
            (
                [id] => 802
                [main] => Clouds
                [description] => scattered clouds
                [icon] => 03d
            )

    )

[base] => cmc stations
[main] => Array
    (
        [temp] => 289.26
        [pressure] => 1013
        [temp_min] => 289.26
        [temp_max] => 289.26
        [humidity] => 30
    )

[wind] => Array
    (
        [speed] => 3.08
        [gust] => 8.74
        [deg] => 252
    )

[clouds] => Array
    (
        [all] => 48
    )

[dt] => 1394612300
[id] => 1851632
[name] => Shuzenji
[cod] => 200

)

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

Comments

1

If you really want to show all keys/values, try:

$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($data));
foreach($iterator as $key => $value) {
    echo "$key => $value<br>";
}

Comments

0
  • $data consists of arrary within an array.
  • So you need to parse it properly.
  • Use nested Foreach.

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.