简体   繁体   English

嵌套数组中的PHP foreach循环

[英]PHP foreach loop in nested array

I'm trying to use an unofficial IMDb api for my project. 我正在尝试为我的项目使用非官方的IMDb api。 I used this piece of code. 我用了这段代码。

<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 {

}
?>

I need to get the values of the "titles" array ONLY. 我只需要获取“titles”数组的值。 (title, id, url) This was the JSON code. (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/
            }
        ]
    }
}

And this is my $array after JSON Decoding. 这是我在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 ) ) ) ) )

Can anyone tell me what I'm doing wrong here? 谁能告诉我这里我做错了什么? I'm getting this error - "Array to String Conversion" 我收到此错误 - “数组到字符串转换”

I have tried solving this for several hours and I've been looking for an answer all over the internet. 我已经尝试解决了几个小时,我一直在寻找互联网上的答案。 Some help will be very much appreciated. 一些帮助将非常感谢。 Thanks. 谢谢。

From what I see, this seems like a json and not an array. 从我看到的,这看起来像一个json而不是一个数组。 In that case, you should do this first: 在这种情况下,您应该首先执行此操作:

$array =  json_decode($array, true);

When it comes to the array traversal, you can try something like this: 当涉及到数组遍历时,你可以尝试这样的事情:

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/>";
} 

Hope this helps. 希望这可以帮助。

Assuming that's JSON data that you're going to json_decode , the data , results , and titles aren't array keys, they're object properties. 假设您要使用json_decode的JSON数据, dataresultstitles不是数组键,它们是对象属性。 Try something like $array->data->results->titles[0]; 尝试类似$array->data->results->titles[0]; . The { character denotes an object property, while [ denotes an array. { character表示对象属性,而[表示数组。

Use 采用

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

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

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