从json数组PHP获取数据

时间:2013-01-19 18:57:44

标签: php json

我使用php json_decode解码了json数据 这是我的输出与print_r

stdClass Object ( 
[uv] => stdClass Object ( 
[current] => stdClass Object ( 
[class_name] => delta-positive 
[display] => 668 [value] => 668 ) 
[yoy_perc] => stdClass Object ( 
         [class_name] => delta-negative 
         [display] => -21.23% 
         [value] => -21.23 ) 
[mom_perc] => stdClass Object ( 
         [class_name] => delta-negative 
         [display] => -8.87% [value] => -8.87 ) 
[yoy] => stdClass Object ( 
         [class_name] => delta-negative 
         [display] => -180 [value] => -180 ) 
[mom] => stdClass Object ( 
         [class_name] => delta-negative 
         [display] => -65 
         [value] => -65 ) ) 
[page_title] => example.com 668 UVs for December 2012 | Compete 
[rank] => stdClass Object ( 
        [current] => stdClass Object ( 
           [class_name] => delta-positive 
             [display] => 1,398,481 
            [value] => 1398481 ) [yoy] => stdClass Object ( [class_name] => delta-negative [display] => -187,667 [value] => -187667 ) [last_month] => stdClass Object ( [class_name] => delta-positive [display] => 1,246,200 [value] => 1246200 ) [mom] => stdClass Object ( [class_name] => delta-negative [display] => -152,281 [value] => -152281 ) [last_year] => stdClass Object ( [class_name] => delta-positive [display] => 1,210,814 [value] => 1210814 ) ) ) 

我需要

[display] => 668 [value] => 668 )

668

我尝试使用foreach但没有运气。任何人都知道如何使用PHP这是一个有效的方式,我的意思是没有循环 谢谢

5 个答案:

答案 0 :(得分:0)

试试这个:

object->uv->current->display;
object->uv->current->value;

或使用json_decode PHP函数。

答案 1 :(得分:0)

$obj->uv->current->value

答案 2 :(得分:0)

尝试将json_decode()与第二个参数集一起使用,以使其输出数组而不是对象结构。

`$outputArray = json_decode($inputString, true);

这将允许您使用foreach()循环播放它。

答案 3 :(得分:0)

如果您对对象感到不舒服,可以将“true”放到json_decode()的第二个参数中。

json_decode($jsondata, true);

因此您可以将它们作为数组访问。

答案 4 :(得分:0)

使用json_decode($ json,true)来获取关联数组。

http://www.php.net/manual/en/function.json-decode.php