winnyboy5
winnyboy5

Reputation: 1516

Fetching array data in PHP

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.......

Upvotes: 0

Views: 113

Answers (3)

bitWorking
bitWorking

Reputation: 12655

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>";
}

Upvotes: 1

Duikboot
Duikboot

Reputation: 1101

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

)

Upvotes: 2

Sumeet Patil
Sumeet Patil

Reputation: 432

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

Upvotes: 0

Related Questions