Stackoverflow API在数组PHP中获得答案

时间:2012-02-07 11:20:13

标签: php arrays json api

我正在尝试使用stackoverflow API,我希望在php数组中获得问题的答案。到目前为止,这是我的PHP代码:

<?php


//KEY
$string = "key=my_key";

//Call stack API .$string
$stack_url = "compress.zlib://http://api.stackoverflow.com/1.1/questions";

//Get and Store API results into a variable
$result = file_get_contents($stack_url);
$jsonArray = json_decode($result);
print_r($jsonArray);
//echo($jsonArray->questions[0]->question_answers_url);
//var_dump($jsonArray);


?>

我想将一个问题的答案存储在一个名为答案的数组中,以便我可以使用for循环访问它们。

我得到的答案是:

stdClass Object
(
    [total] => 2618591
    [page] => 1
    [pagesize] => 30
    [questions] => Array
        (
            [0] => stdClass Object
                (
                    [tags] => Array
                        (
                            [0] => c#
                            [1] => ssh
                            [2] => openssh
                            [3] => rsacryptoserviceprovider
                        )

                    [answer_count] => 1
                    [favorite_count] => 0
                    [question_timeline_url] => /questions/9164203/timeline
                    [question_comments_url] => /questions/9164203/comments
                    [question_answers_url] => /questions/9164203/answers
                    [question_id] => 9164203
                    [owner] => stdClass Object
                        (
                            [user_id] => 311966
                            [user_type] => registered
                            [display_name] => simonc
                            [reputation] => 301
                            [email_hash] => 021f3344004f0c886d715314fa02037d
                        )

                    [creation_date] => 1328548627
                    [last_edit_date] => 1328611688
                    [last_activity_date] => 1328611688
                    [up_vote_count] => 0
                    [down_vote_count] => 0
                    [view_count] => 25
                    [score] => 0
                    [community_owned] => 
                    [title] => Format of PKCS private keys
                )

            [1] => stdClass Object
                (
                    [tags] => Array
                        (
                            [0] => c#
                            [1] => .net
                            [2] => combobox
                        )

                    [answer_count] => 3
                    [favorite_count] => 0
                    [question_timeline_url] => /questions/9174765/timeline
                    [question_comments_url] => /questions/9174765/comments
                    [question_answers_url] => /questions/9174765/answers
                    [question_id] => 9174765
                    [owner] => stdClass Object
                        (
                            [user_id] => 1194399
                            [user_type] => registered
                            [display_name] => Goxy
                            [reputation] => 1
                            [email_hash] => 5fc8c96b6b85c6339cb9ac4ab60cb247
                        )

                    [creation_date] => 1328611202
                    [last_activity_date] => 1328611686
                    [up_vote_count] => 0
                    [down_vote_count] => 0
                    [view_count] => 15
                    [score] => 0
                    [community_owned] => 
                    [title] => WPF: Bind simple List<myClass> to Combobox
                )
....

1 个答案:

答案 0 :(得分:0)

不确定要提取哪个属性,但我认为它是'question_answers_url'。

$answersArray = Array();
for($i=0;$i<count($jsonArray['questions']);$i++){
    //assuming it is the 'question_answers_url' property that you want
    array_push($answersArray,$jsonArray['questions'][$i]['question_answers_url']);
}

应该这样做。