如何从这个php数组中获取价值

时间:2015-10-22 08:51:16

标签: php arrays

我有来自json_decode的这个数组

stdClass Object ( 
         [coord] => stdClass Object ( 
                             [lon] => 8.97 
                             [lat] => 51 
                            ) 
         [weather] => Array ( 
                          [0] => stdClass Object ( 
                               [id] => 804 
                               [main] => Clouds 
                               [description] => overcast clouds 
                               [icon] => 04d 
                             ) 
                         ) 
          [base] => stations 
          [main] => stdClass Object ( 
                        [temp] => 281.17 
                        [pressure] => 1014 
                        [humidity] => 87 
                        [temp_min] => 280.93 
                        [temp_max] => 281.48 
                       ) 
           [visibility] => 7000 
           [wind] => stdClass Object ( 
                        [speed] => 3.6 
                        [deg] => 180 
                      ) 
           [clouds] => stdClass Object ( 
                        [all] => 90 
                      ) 
           [dt] => 1445499854 
           [sys] => stdClass Object ( 
                        [type] => 1 
                        [id] => 4954 
                        [message] => 0.0056 
                        [country] => DE 
                        [sunrise] => 1445493535 
                        [sunset] => 1445530644 
                       ) 
            [id] => 2906244 
            [name] => Herbelhausen 
            [cod] => 200 
         )

我从中获得了描述

$response_a2->weather[0]->description

但我正试图获得温度,压力,湿度或速度

$response_a2->base->temp

3 个答案:

答案 0 :(得分:1)

您正在寻找

$response_a2->main->temp

感谢 RiggsFolly 来美化阵列。

下次,编写如下代码:

echo "<pre">;
print_r($response_a2);
echo "</pre>";

并且您的数组在浏览器中看起来很漂亮

答案 1 :(得分:0)

$response_a2->base->temp

中不存在该数据

相反,我们

$response_a2->main->temp
$response_a2->main->presure
$response_a2->main->humidity

答案 2 :(得分:0)

您向我们展示的是一个对象,而不是一个数组。对象与数组的工作方式不同他们使用->运算符来达到某些值。

例如,假设我有对象$obj,我想达到价值['some_value']

echo $obj->some_value;