<!-- 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:
标准类对象 ( [查询] => 标准类对象 ( [计数] => 1 [创建] => 2016-05-05T19:15:02Z [lang] => en-US [结果] => 标准类对象 ( [通道] => 标准类对象 ( [单位] => 标准类对象 ( [距离] => mi [压力] => 英寸 [速度] =>英里/小时 [温度] => 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 ) )
为了扩展我上面的答案...
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 . " ";
}
}
现在,如果事实证明键"距离"的值不是字符串,它将对您产生不同的错误。