4
Contributors
4
Replies
23
Views
3 Years
Discussion Span
Last Post by arafath077
0

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

1

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']}' />";
}

Edited by diafol

1

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.