繁体   English   中英

从json_decode获取单个元素

[英]Fetch single element from json_decode

我有以下代码:

<?php
    $fql  = "SELECT url, normalized_url, share_count, like_count, comment_count, ";
    $fql .= "total_count, commentsbox_count, comments_fbid, click_count FROM ";
    $fql .= "link_stat WHERE url = 'http://apple.com'";

    $apifql = "https://api.facebook.com/method/fql.query?format=json&query=".urlencode($fql);
    $json = file_get_contents($apifql);
    $obj = json_decode($json);
    print_r($obj);
?>

输出:

Array ( [0] => stdClass Object ( [url] => http://apple.com [normalized_url] => http://www.apple.com/ [share_count] => 366493 [like_count] => 514795 [comment_count] => 298452 [total_count] => 1179740 [commentsbox_count] => 0 [comments_fbid] => 381975869314 [click_count] => 16558 ) )

如何仅将commentsbox_count定义为字符串? 我的数据库需要它。

您可以通过$obj[0]->commentsbox_count访问它。

顶级元素是一个单元素数组,唯一的键是0 该元素是一个stdClass对象(非常类似于数组,但是具有属性而不是数组元素),因此您可以使用->name来访问该元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM