嵌套数组中的PHP foreach循环

时间:2016-04-05 16:48:36

标签: php multidimensional-array foreach

我正在尝试为我的项目使用非官方的IMDb api。我用了这段代码。

<form name="search-imdb" autocomplete="off" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="q" title="Type Name, Title, Character etc and hit Enter" placeholder="Type Name, Title, Character etc and hit Enter" size="50" />
<input type="submit" name="submit" title="Click to Fetch Data from IMDb" value="Search IMDb" />
</form>

<?php

if (isset($_POST['submit'])){
    $user_search = $_POST['q'];
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "imdb.wemakesites.net/api/search?q=".$user_search); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    $array = json_decode($output, true);
    print_r($array);

   foreach ($array as $key => $data){
    if(is_array($data)){
        foreach($data as $subkey => $results){
            if(is_array($results)){
                foreach($results as $key => $titles){
                    echo $titles."<br />";
                }
            } else {
                echo $results."<br />";
            }
        }
    } else {
        echo $data."<br />";
    }
}
    curl_close($ch);
} else {

}
?>

我只需要获取“titles”数组的值。 (title,id,url)这是JSON代码。

Array{
"status": "success",
"code": 200,
"message": "ok",
"term": "jQuery21409989625962002346_1459839763205&q",
"search_url":"http://www.imdb.com/find?q=batmansuperman&s=all5703b359add1b",
"data": {
    "results": {
        "titles": [
            {
                "title": "Batman",
                "id": "tt0096895",
                "url":"http://www.imdb.com/title/tt0096895/"
            },
            {
                "title": "Batman",
                "id": "tt0059968",
                "url":"http://www.imdb.com/title/tt0059968/"
            },
            {
                "title" :"Batman v Superman: Dawn of Justice",
                "id": "tt2975590",
                "url":"http://www.imdb.com/title/tt2975590/"
            },
            {
                "title": "Batman Begins",
                "id": "tt0372784",
                "url":"http://www.imdb.com/title/tt0372784/
            }
        ]
    }
}

这是JSON解码后的$数组。

Array ( [status] => success [code] => 200 [message] => ok [term] => megamind [search_url] => http://www.imdb.com/find?q=megamind&s=all5703f4f88841f [data] => Array ( [results] => Array ( [titles] => Array ( [0] => Array ( [title] => Megamind [id] => tt1001526 [url] => http://www.imdb.com/title/tt1001526/?ref_=fn_al_tt_1 ) [1] => Array ( [title] => Megamind [id] => tt1785464 [url] => http://www.imdb.com/title/tt1785464/?ref_=fn_al_tt_2 ) [2] => Array ( [title] => MegaMind [id] => tt1890468 [url] => http://www.imdb.com/title/tt1890468/?ref_=fn_al_tt_3 ) [3] => Array ( [title] => Mega Mindy [id] => tt0891395 [url] => http://www.imdb.com/title/tt0891395/?ref_=fn_al_tt_4 ) [4] => Array ( [title] => Megamind [id] => tt3022564 [url] => http://www.imdb.com/title/tt3022564/?ref_=fn_al_tt_5 ) [5] => Array ( [title] => Megamind: The Button of Doom [id] => tt1847645 [url] => http://www.imdb.com/title/tt1847645/?ref_=fn_al_tt_6 ) [6] => Array ( [title] => Megamind [id] => tt3624292 [url] => http://www.imdb.com/title/tt3624292/?ref_=fn_al_tt_7 ) [7] => Array ( [title] => Megamind [id] => tt2173285 [url] => http://www.imdb.com/title/tt2173285/?ref_=fn_al_tt_8 ) [8] => Array ( [title] => Mega Mindy Versus ROX [id] => tt4706602 [url] => http://www.imdb.com/title/tt4706602/?ref_=fn_al_tt_9 ) [9] => Array ( [title] => Aa Megamisama [id] => tt0872301 [url] => http://www.imdb.com/title/tt0872301/?ref_=fn_al_tt_10 ) ) [characters] => Array ( [0] => Array ( [title] => Megamind [id] => ch0194198 [url] => http://www.imdb.com/character/ch0194198/?ref_=fn_al_ch_1 ) [1] => Array ( [title] => Megamind's Mother [id] => ch0229958 [url] => http://www.imdb.com/character/ch0229958/?ref_=fn_al_ch_2 ) [2] => Array ( [title] => Megamind's Father [id] => ch0229956 [url] => http://www.imdb.com/character/ch0229956/?ref_=fn_al_ch_3 ) ) [names] => Array ( [0] => Array ( [title] => Megamind [id] => nm6292338 [url] => http://www.imdb.com/name/nm6292338/?ref_=fn_al_nm_1 ) ) [keywords] => Array ( [0] => Array ( [title] => megami [id] => megami [url] => http://www.imdb.com/keyword/megami/?ref_=fn_al_kw_1 ) ) [companies] => Array ( [0] => Array ( [title] => Megami [id] => co0544394 [url] => http://www.imdb.com/company/co0544394/?ref_=fn_al_co_1 ) [1] => Array ( [title] => Mega Mind Media [id] => co0558438 [url] => http://www.imdb.com/company/co0558438/?ref_=fn_al_co_2 ) [2] => Array ( [title] => Elm Tree Gaming [id] => co0202064 [url] => http://www.imdb.com/company/co0202064/?ref_=fn_al_co_3 ) ) ) ) )

谁能告诉我这里我做错了什么?我收到此错误 - “数组到字符串转换”

我已经尝试解决了几个小时,我一直在寻找互联网上的答案。一些帮助将非常感谢。感谢。

3 个答案:

答案 0 :(得分:4)

从我看来,这看起来像一个json而不是一个数组。在这种情况下,您应该首先执行此操作:

$array =  json_decode($array, true);

当涉及到数组遍历时,你可以尝试这样的事情:

foreach($array['data']['results']['titles'] as $data) {
    echo "Title:".$data['title'];
    echo "<br/>";
    echo "ID:".$data['id'];
    echo "<br/>";
    echo "URL:".$data['url'];
    echo "<hr/>";
} 

希望这有帮助。

答案 1 :(得分:2)

假设您将要{J}数据json_decodedataresultstitles不是数组键,它们就是对象属性。尝试类似$array->data->results->titles[0];的内容。 {字符表示对象属性,而[表示数组。

答案 2 :(得分:2)

使用

$json_array = json_encode($array['data']['results']['titles']);
    print_r($json_array);