使用 PHP 的嵌套数组 JSON

我在弄清楚如何在 JSON 中获取嵌套数组时遇到问题。我正在使用 PHP 和 foreach 循环来获取它。我想在子类别数组下获取标题等。我会怎么做?这个 json 中有很多嵌套数组。


我试过的:我用 php 检查了这个网站的嵌套数组。我已经研究了嵌套的工作方式,但它对我不起作用。


这是我的代码:


                    $json_array = (array) json_decode($response, true);

                    foreach($json_array['bannerTiles'] as $json)

                    {




                                echo $json_array['bannerTiles']['categories']['subcategories'][0] 

                                     ['tiles']['text'];


                    }


弑天下
浏览 240回答 2
2回答

神不在的星期二

根据 json_decode 的文档,如果您使用将返回数组的第二个参数“TRUE”:https://www.php.net/manual/en/function.json-decode.php因此,无需使用 (array) json_decode($response, TRUE) 进行类型转换。以下会很好:$json_array = json_decode($response, TRUE);其次,当我将您在上面提供的响应剪切并粘贴到 IDE 中时,它会随着全部退出而损坏。根据我上面提供的文档链接,响应应该具有如下结构:array(1) {  [1]=>  array(2) {    ["English"]=>    array(2) {      [0]=>      string(3) "One"      [1]=>      string(7) "January"    }    ["French"]=>    array(2) {      [0]=>      string(3) "Une"      [1]=>      string(7) "Janvier"    }  }}获取一个 JSON 字符串(开始时格式正确)并将其解码为一个数组将始终看起来像这样。您的回复没有,这让我相信(没有看到)您的 $response 实际上不是格式正确的 JSON。编辑:在下面的评论中查看我的评论。但这里的底线是你试图在 JSON 上进行 foreach。这里没有什么可寻找的。它将被放入数组中的 JSON 数据。无需调教就可以调出这些钥匙,您将成为金色!聊天后:OP 和我聊了聊,最后得到了这个超级嵌套的 foreach 循环:$decoded = json_decode( $json, TRUE );    foreach ( $decoded['categories'] as $category ) {        foreach ( $category as $subcat ) {            if ( is_array( $subcat ) ) {                foreach ( $subcat as $s ) {                    foreach ( $s as $tiles ) {                        foreach ( $tiles as $tile ) {                            foreach ( $tile as $t ) {                                // Do stuff                            }                        }                    }                }            } else {                echo $subcat;            }        }    }

大话西游666

因为你的 json 字符串很麻烦,你可以试试这个代码:$str=""; // your json string$date = date("Y/m/d");$json_array = json_decode($str, true);foreach($json_array['bannerTiles'] as $json){&nbsp; &nbsp; if(isset($json['webSearchUrl'])) echo "<br>Web URL: ". $json['webSearchUrl']."<br>";&nbsp;&nbsp;&nbsp; &nbsp; echo "<b>"."Published on: ".$date."<br>";&nbsp; &nbsp; if(isset($json['displayText'])) echo "<b>Title: ".$json['displayText']."<br>";}foreach($json_array['categories'] as $categories_1){&nbsp; &nbsp; foreach($categories_1 as $categories_2)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; foreach($categories_2 as $categories_3)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach($categories_3['tiles'] as $tile)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(isset($tile['query'])) echo "<br>Web URL: ". $tile['query']['webSearchUrl']."<br>";&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<b>"."Published on: ".$date."<br>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(isset($tile['query'])) echo "<b>Title: ".$tile['query']['displayText']."<br>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(isset($tile['image']))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<b>"."<img class='img-responsive' src =".$tile['image']&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;['thumbnailUrl']."></a><br>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(isset($tile['query']))&nbsp; echo $tile['query']['text'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP