<!-- 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对象 ( [query] =&gt; stdClass对象 ( [count] =&gt; 1 [created] =&gt; 2016-05-05T19:15:02Z [lang] =&gt; EN-US [results] =&gt; stdClass对象 ( [channel] =&gt; stdClass对象 ( [单位] =&gt; stdClass对象 ( [距离] =&gt; MI [压力] =&gt;在 [speed] =&gt;英里 [温度] =&gt; 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
)
)
答案 0 :(得分:0)
扩展我上面的答案......
foreach的工作方式有两种:
1)
foreach($variable as $thing)
{
// Do something
}
2)
foreach($variable as $index=>$thing)
{
// Do something
}
在两个示例中,$ variable必须是数组或对象,并且它将迭代$ variable。
在第一个例子中,$ thing将是当前值(对象或数组的当前元素)。在第二个示例中,$ index是对象或数组的索引(并且它是自动分配的),$ thing是当前值(同样是对象或数组的当前元素。)
所以,让我们说这是你的数组
$variable = ['foo', 'bar', 'baz'];
foreach($variable as $thing)
{
echo $thing.'<br/>';
}
你会得到:
foo
bar
baz
如果您使用了第二版foreach ....
$variable = ['foo', 'bar', 'baz'];
foreach($variable as $index=>$thing)
{
echo 'Element '. $index . ' = ' $thing.'<br/>';
}
你会得到:
Element 0 = foo
Element 1 = bar
Element 2 = baz
第二种foreach形式的重要内容是$ index将永远成为一个数组。它可能是一个数字索引,它可能是一个字符串索引。它永远不会是一个数组。
因此,在您的示例中,$ key ['distance']将始终说它是非法偏移量,因为$ key不是一个开头的数组。
修改强> 使用上面的示例:
foreach ($obj->query->results->channel->units as $key => $value) {
if($key == 'distance') {
echo $key . " " . "<br><br>";
echo $value . " ";
}
}
现在,如果事实证明键'distance'的值不是字符串,那么它将会有不同的错误。