我正在使用竞争api来检索网站的唯一访问者。这是代码......
$activityurl = file_get_contents("http://apps.compete.com/sites/facebook.com/trended/uv/?apikey=myapikey");
$jsondecoded = json_decode($activityurl);
foreach($jsondecoded as $values)
{
print_r($values)."<br />";
}
这是我得到的输出..
OKstdClass Object ( [trends] => stdClass Object ( [uv] => Array ( [0] => stdClass Object ( [date] => 201210 [value] => 160962051 ) [1] => stdClass Object ( [date] => 201211 [value] => 163640791 ) [2] => stdClass Object ( [date] => 201212 [value] => 165602717 ) [3] => stdClass Object ( [date] => 201301 [value] => 166238881 ) [4] => stdClass Object ( [date] => 201302 [value] => 165056717 ) [5] => stdClass Object ( [date] => 201303 [value] => 164808579 ) [6] => stdClass Object ( [date] => 201304 [value] => 164131054 ) [7] => stdClass Object ( [date] => 201305 [value] => 163323608 ) [8] => stdClass Object ( [date] => 201306 [value] => 163078120 ) [9] => stdClass Object ( [date] => 201307 [value] => 163864002 ) [10] => stdClass Object ( [date] => 201308 [value] => 163413311 ) [11] => stdClass Object ( [date] => 201309 [value] => 164616160 ) [12] => stdClass Object ( [date] => 201310 [value] => 167403281 ) ) )
我想从上面的
中检索'value'答案 0 :(得分:0)
你可以这样做:
$jsondecoded = json_decode($activityurl, true);
第二个参数意味着:
当为TRUE时,返回的对象将被转换为关联数组。
我希望你知道如何使用数组:)
答案 1 :(得分:0)
您需要在循环中提取它们
$activityurl = file_get_contents("http://apps.compete.com/sites/facebook.com/trended/uv/?apikey=myapikey");
$jsondecoded = json_decode($activityurl);
$values = array();
if ($jsondecoded->status === 'ACCESS_DENIED') throw new Exception($jsondecoded->status_message);
foreach($jsondecoded->data->trends->uv as $object)
{
$values[] = $object->value;
}
var_dump($values)
给出了
array(13) {
[0] =>
int(160962051)
[1] =>
int(163640791)
[2] =>
int(165602717)
[3] =>
int(166238881)
[4] =>
int(165056717)
[5] =>
int(164808579)
[6] =>
int(164131054)
[7] =>
int(163323608)
[8] =>
int(163078120)
[9] =>
int(163864002)
[10] =>
int(163413311)
[11] =>
int(164616160)
[12] =>
int(167403281)
}
答案 2 :(得分:0)
$jsondecoded = json_decode($activityurl,true); //u will get array type
然后你可以做到
$trends=$jsondecoded['trends'];
$uv=$trends['uv'];
foreach $uv as $target
{
echo $target['data'].' '. $target['value'].'<br/>';
}