【问题标题】:PHP foreach loop in nested array嵌套数组中的PHP foreach循环
【发布时间】:2016-07-25 18:02:35
【问题描述】:

我正在尝试为我的项目使用非官方的 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。

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

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

我已经尝试解决这个问题几个小时,并且一直在整个互联网上寻找答案。一些帮助将不胜感激。谢谢。

【问题讨论】:

  • 您想在这里打印什么?那也是哪种格式?请先说明。
  • 您应该使用文本而不是图像来存储此类数据,以便人们可以轻松查看和使用它来帮助您。 Also your data seems to be JSON, then use json_decode($data, true); to put it into an array
  • 首先......一个名为array的数组就像一只名叫Dog的狗。朋友,请为您的阵列取一个有意义的名称。
  • 你真的有这些索引吗?请发布完整数组的 print_r,以便我们了解它的组织方式。
  • 这不是一个数组,而是 JSON。甚至不是有效的 JSON。您是直接从 API 接收的吗?或者您在此处发布之前编辑了答案?

标签: php multidimensional-array foreach


【解决方案1】:

在我看来,这似乎是一个 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/>";
} 

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    假设您要转到 json_decode 的 JSON 数据,dataresultstitles 不是数组键,它们是对象属性。试试$array-&gt;data-&gt;results-&gt;titles[0]; 之类的东西。 { 字符表示对象属性,而[ 表示数组。

    【讨论】:

      【解决方案3】:

      使用

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-19
        • 2015-07-06
        • 1970-01-01
        • 2015-07-17
        • 2013-07-05
        相关资源
        最近更新 更多