PHP foreach 只返回嵌套数组的第一个 [0]

我有一个像下面这样的对象,数组相互嵌套。我使用 foreach 循环来遍历数组的第一层和第二层。


array (

   'totalHits' => 500,

   'total'     => 845,

   'hits' =>

   array (

       0 => array (

              'url' => 'www.someurl.com',

              'id'  => '11',

       ),

       1 => array (

              'url' => 'www.differenturl.com',

              'id'  => '22',

       ),

 );

我试图从嵌套在“hits”中的所有数组中获取键和值,但我只得到第一个,hits[0]。我错过了什么?


<?php 

    if($_SERVER['REQUEST_METHOD']=='POST'){

        

        $keywords       = $_POST['pixa_keyword'];           

        $api_key        = "my_hidden_api_key";

        $url            = "https://pixabay.com/api/?key=".$api_key."&safesearch=true&q=".$keywords;

        $json           = file_get_contents($url);

        

        

    }

    

    

?>


<body>

    


<form action="#" method="post">

    <input name="pixa_keyword" id="cms_pixaSearchKeywords2" type="text" value="">       

    <input name="submit_pixa_search" id="cms_pixaSearchBtn2" type="submit" value="search">

</form>



<div id="cms_displayPixaResults"></div>

    

<?php 

    

    $pixa_feedback=[];

    

    

    if($_SERVER['REQUEST_METHOD']=="POST"){

        $pixa_feedback = json_decode($json);

    }

    


    foreach($pixa_feedback as $inner){

        // check type

        if(is_array($inner)){

            // iterate through nested array

            $i = 0;

            foreach ($inner[$i] as $key => $value){      

                echo $key . ": " . $value . " <br>";

                $i++;

            }

        }

    }

    

    

?>


胡说叔叔
浏览 138回答 3
3回答

繁星coding

您的计数器变量没有做任何事情,因为您foreach只会迭代$inner[0]然后停止。您需要添加第三级迭代:foreach($pixa_feedback as $inner){&nbsp; &nbsp; // check type&nbsp; &nbsp; if(is_array($inner)){&nbsp; &nbsp; &nbsp; &nbsp; // iterate through nested array&nbsp; &nbsp; &nbsp; &nbsp; foreach ($inner as $values){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($values as $key => $value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $key . ": " . $value . " <br>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}输出:url: www.someurl.com <br>id: 11 <br>url: www.differenturl.com <br>id: 22 <br>

ibeautiful

你没有正确循环你的数组试试这个:$temp = array (&nbsp; &nbsp;'totalHits' => 500,&nbsp; &nbsp;'total'&nbsp; &nbsp; &nbsp;=> 845,&nbsp; &nbsp;'hits' =>&nbsp; &nbsp;array (&nbsp; &nbsp; &nbsp; &nbsp;0 => array (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'url' => 'www.someurl.com',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'id'&nbsp; => '11',&nbsp; &nbsp; &nbsp; &nbsp;),&nbsp; &nbsp; &nbsp; &nbsp;1 => array (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'url' => 'www.differenturl.com',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'id'&nbsp; => '22',&nbsp; &nbsp; &nbsp; &nbsp;),&nbsp;));foreach($temp["hits"] as $inner_arr){&nbsp; &nbsp; &nbsp;echo $inner_arr["url"];&nbsp; &nbsp; &nbsp;echo $inner_arr["id"];}

哆啦的时光机

在 a 中,foreach您不需要计数器变量。循环体将针对第一个“参数”中的每个项目运行。因此内循环(在外循环的第 3 次迭代期间)迭代array ('url' => 'www.someurl.com', 'id' &nbsp;=> '11')并在之后完成。
打开App,查看更多内容
随时随地看视频慕课网APP