Im using wunderground.com weather API.
I got JSON to an array.buy i coudlnt echo arrray one by one.
these the sample code to i used in my site
$json = file_get_contents("http://api.wunderground.com/api/ab3f1aefcf4033d4/forecast10day/q/CA/San_Francisco.json"); //$phparrya = json_decode($jsonstring); $array_json=json_decode($json, true); print_r($obj);

This problem is actually cause by the data is not in correct format. As the data provided is in {features: {forecast10day: 1 }} format while the json decode function accept {"features": {"forecast10day": 1}}

Member Avatar for diafol

What data did you want to extract? The textforecast/forecast data array would be:

$array_json['forecast']['txt_forecast']['forecastday']

For a 10 day forecast this contains 20 items - one daytime and one night for each day. It is number-indexed (0-19)

The simpleforecast/forecast data array would be:

$array_json['forecast']['txt_forecast']['simpleforecast']

For a 10 dat forecast, this contains 10 items - one whole day forecast for each day. It is number-indexed (0-9)

SO you could do...

$textfc = $array_json['forecast']['txt_forecast']['forecastday'];
$simplefc = $array_json['forecast']['txt_forecast']['simpleforecast'];

foreach($simplefc as $day)
{
    ...format items as you wish...
    $icon = "<img src='{$day['icon_url']}' />";
}

Hi,
Try to workaround this code:

<?php

$json = file_get_contents("San_Francisco.json"); 
$array_json=json_decode($json, true); 
//print_r($array_json);

display_jsonarray($array_json);    




function display_jsonarray($array, $lvl_indent = 0)
{
    $indent = "";
    $istr = "    ";
    if($lvl_indent > 0)
    {
        $indent = str_repeat($istr,$lvl_indent);
    }      
    foreach ($array as $key => $value)
    {
        if(is_array($value))
        {
            echo $indent.$key.": \n<br/>";
            $lvl_indent = $lvl_indent + 4;
            display_jsonarray($value,$lvl_indent);
        }    
        else
        {
            echo $indent.$key.": ".$value."\n";
        }

    }
} 


?>

With the display_jsonarray function i try to display recursiv the array but identation is'nt ok.
But maybe this code help you.

thanks u all..worked

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.