<!-- I want to see the content from units tag at json file, and its don't work. -->
1.
$json = file_get_contents('https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22' . "$oras" . '%2C%20' . "$tara" . '%22%20)%20and%20u%3D' . "%27$afisaj%27" . '&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys');
$obj = json_decode($json);
echo "<pre>";
print_r($obj);
echo "</pre>";
echo "<br><hr><br>";
foreach ($obj->query->results->channel->units as $key => $value) {
var_dump($key);
echo $key['distance']; // there is the problem
echo $key . " " . "<br><br>";
echo $value . " ";
}
echo "<br><hr><br>";
foreach ($obj->query->results->channel->item->forecast as $key) {
echo '<b style="color:red;">Low:</b>' . $key->low .
'<b style="color:blue;">High:</b>' . $key->high .
'<b style="color:green;">Date</b>' . $key->date .
'<b style="color:purple;">Day</b>' . $key->day .
'<b style="color:yellow;">Text</b>' . $key->text .
"<br>";
}
?>
The content at $obj, i want to obtain data from units:
stdClass Object ( [query] => stdClass Object ( [count] => 1 [created] => 2016-05-05T19:15:02Z [lang] => en-US [results] => stdClass Object ( [channel] => stdClass Object ( [units] => stdClass Object ( [distance] => mi [pressure] => in [speed] => mph [temperature] => F )
[title] => Yahoo! Weather - Nome, AK, US
[link] => http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-2460286/
[description] => Yahoo! Weather for Nome, AK, US
[language] => en-us
[lastBuildDate] => Thu, 05 May 2016 11:15 AM AKDT
[ttl] => 60
[location] => stdClass Object
(
[city] => Nome
[country] => United States
[region] => AK
)
[wind] => stdClass Object
(
[chill] => 30
[direction] => 68
[speed] => 7
)
[atmosphere] => stdClass Object
(
[humidity] => 95
[pressure] => 1006.0
[rising] => 0
[visibility] => 10.6
)
[astronomy] => stdClass Object
(
[sunrise] => 6:13 am
[sunset] => 11:45 pm
)
[image] => stdClass Object
(
[title] => Yahoo! Weather
[width] => 142
[height] => 18
[link] => http://weather.yahoo.com
[url] => http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif
)
[item] => stdClass Object
(
[title] => Conditions for Nome, AK, US at 10:00 AM AKDT
[lat] => 64.499474
[long] => -165.405792
[link] => http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-2460286/
[pubDate] => Thu, 05 May 2016 10:00 AM AKDT
[condition] => stdClass Object
(
[code] => 26
[date] => Thu, 05 May 2016 10:00 AM AKDT
[temp] => 34
[text] => Cloudy
)
[forecast] => Array
(
[0] => stdClass Object
(
[code] => 28
[date] => 05 May 2016
[day] => Thu
[high] => 43
[low] => 31
[text] => Mostly Cloudy
)
[1] => stdClass Object
(
[code] => 34
[date] => 06 May 2016
[day] => Fri
[high] => 43
[low] => 33
[text] => Mostly Sunny
)
[2] => stdClass Object
(
[code] => 28
[date] => 07 May 2016
[day] => Sat
[high] => 39
[low] => 33
[text] => Mostly Cloudy
)
[3] => stdClass Object
(
[code] => 26
[date] => 08 May 2016
[day] => Sun
[high] => 38
[low] => 31
[text] => Cloudy
)
[4] => stdClass Object
(
[code] => 39
[date] => 09 May 2016
[day] => Mon
[high] => 38
[low] => 37
[text] => Scattered Showers
)
[5] => stdClass Object
(
[code] => 12
[date] => 10 May 2016
[day] => Tue
[high] => 38
[low] => 36
[text] => Rain
)
[6] => stdClass Object
(
[code] => 26
[date] => 11 May 2016
[day] => Wed
[high] => 38
[low] => 34
[text] => Cloudy
)
[7] => stdClass Object
(
[code] => 28
[date] => 12 May 2016
[day] => Thu
[high] => 37
[low] => 33
[text] => Mostly Cloudy
)
[8] => stdClass Object
(
[code] => 5
[date] => 13 May 2016
[day] => Fri
[high] => 37
[low] => 33
[text] => Rain And Snow
)
[9] => stdClass Object
(
[code] => 30
[date] => 14 May 2016
[day] => Sat
[high] => 47
[low] => 31
[text] => Partly Cloudy
)
)
To expand on my answer above...
foreach works one of two ways:
1)
foreach($variable as $thing)
{
// Do something
}
2)
foreach($variable as $index=>$thing)
{
// Do something
}
In both examples, $variable must be an array or object, and it will iterate over $variable.
In the first example, $thing will be the current value (the current element of the object or array). In the second example, $index is the index of the object or array (and it is automatically assigned), and $thing is the current value (again, the current element of the object or array.)
So, let's say this is your array
$variable = ['foo', 'bar', 'baz'];
foreach($variable as $thing)
{
echo $thing.'<br/>';
}
You would get:
foo
bar
baz
If instead you used the second version of foreach....
$variable = ['foo', 'bar', 'baz'];
foreach($variable as $index=>$thing)
{
echo 'Element '. $index . ' = ' $thing.'<br/>';
}
You'd get:
Element 0 = foo
Element 1 = bar
Element 2 = baz
The important takeaway from the second form of foreach is that $index will never be an array. It might be a numeric index, it might be a string index. It will never be an array.
So, in your example $key['distance'] will always say that it's an illegal offset, because $key is not an array to begin with.
Edit: Using your example above:
foreach ($obj->query->results->channel->units as $key => $value) {
if($key == 'distance') {
echo $key . " " . "<br><br>";
echo $value . " ";
}
}
Now, if it turns out the value for the key 'distance' is not a string, it's going to have a different error for you.