从数组中选择元素(?)(api)

时间:2014-12-18 23:36:22

标签: php arrays object reddit

我从PHP开始,我正在尝试从subreddit获取信息 - 现在,它已经有效,但我收到的是:

Object ( stdClass Object ( [kind] => Listing [data] => stdClass Object ( [modhash] => lkxhn9w3ww19149721e2f6ff1071cedb82f577a7a556a5e9cf [children] => Array ( [0] => stdClass Object ( [kind] => t3 [data] => stdClass Object ( [domain] => self.leagueoflegends [banned_by] => [media_embed] => stdClass Object ( ) [subreddit] => leagueoflegends [selftext_html] => [selftext] => [likes] => [user_reports] => Array ( ) [secure_media] => [link_flair_text] => [id] => 2papx0 [gilded] => 0 [secure_media_embed] => stdClass Object ( ) [clicked] => [report_reasons] => [author] => Azberg [media] => [score] => 2174 [approved_by] => [over_18] => [hidden] => [thumbnail] => self [subreddit_id] => t5_2rfxx [edited] => [link_flair_css_class] => [author_flair_css_class] => gnar [downs] => 0 [mod_reports] => Array ( ) [saved] => [is_self] => 1 [name] => t3_2papx0 [permalink] => /r/leagueoflegends/comments/2papx0/would_you_like_riot_to_stream_lcs_in_60_fps_like/ [stickied] => [created] => 1418623777 [url] => http://www.reddit.com/r/leagueoflegends/comments/2papx0/would_you_like_riot_to_stream_lcs_in_60_fps_like/ [author_flair_text] => [title] => Would you like Riot to stream LCS in 60 FPS like ESL? [created_utc] => 1418594977 [ups] => 2174 [num_comments] => 421 [visited] => [num_reports] => [distinguished] => ) ) ) [after] => t3_2papx0 [before] => ) ) 

我想知道如何从中选择数据 - 我的意思是,如果我只想要网址或只是标题?我尝试将它作为代码处理,但它没有用。对不起我的英语,这不是我的主要语言。

$reddit = new reddit($USER, $PASS);
print_r($reddit);
print_r($reddit->getListing("leagueoflegends", 1));

最后一行是打印信息的内容。我想知道如何只选择它的一些元素 - 比如URL,种类或modhash。 提前谢谢!

1 个答案:

答案 0 :(得分:1)

要访问它,您需要浏览对象/数组以获取所需的键。 在上面的示例中,这将:

$data = $reddit->getListing("leagueoflegends", 1);   
echo $data->data->children[0]->data->url;

您最终可能会获得更多children,因此您可能需要使用foreach循环来查看它们,如下所示:

foreach($data->data->children as $child) {
    echo $child->data->url;
}