1

I am having a problem with figuring out how to get nested array in JSON. I am using PHP and foreach loop to get it. I want to get the titles, etc..under sub categories array. How would I be doing that? There are like tons of nested array in this json.

What I have tried: I have check this website nested array with php. I have looked at how the nested work but its not working for me.

Here is my code:

                    $json_array = (array) json_decode($response, true);
                    foreach($json_array['bannerTiles'] as $json)
                    {



                                echo $json_array['bannerTiles']['categories']['subcategories'][0] 
                                     ['tiles']['text'];

                    }

Here is the JSON output:

 {
"_type": "TrendingVideos",
"instrumentation": {
    "_type": "ResponseInstrumentation"
},
"bannerTiles": [{
    "query": {
        "text": "Good As Hell Lizzo",
        "displayText": "Good As Hell Lizzo",
        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Good%20As%20Hell%20Lizzo&FORM=VSTREQ"
    }

}, {


}],
"categories": [{
        "title": "Music videos",
        "subcategories": [{
                "tiles": [{
                        "query": {
                            "text": "Goodbyes Post Malone",
                            "displayText": "Goodbyes Post Malone",
                            "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Goodbyes%20Post%20Malone&FORM=VSTREQ"
                        }



                    },
                    {

                    },
                    {

                    },
                    {
                        "query": {
                            "text": "VIBEZ DaBaby",
                            "displayText": "VIBEZ DaBaby",
                            "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=VIBEZ%20DaBaby&FORM=VSTREQ"
                        },
                        "image": {
                            "description": "Image from: complex.com",
                            "thumbnailUrl": "https:\/\/tse2.mm.bing.net\/th?id=OET.a36022146bab42259af097b4d8ad66e6&pid=Api",
                            "contentUrl": "https:\/\/www.complex.com\/music\/2019\/07\/dababy-everything-you-need-to-know\/"
                        }
                    },
                    {
                        "query": {
                            "text": "Bandit Juice WRLD",
                            "displayText": "Bandit Juice WRLD",
                            "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Bandit%20Juice%20WRLD&FORM=VSTREQ"
                        },
                        "image": {
                            "description": "Image from: nypost.com",
                            "thumbnailUrl": "https:\/\/tse4.mm.bing.net\/th?id=OET.2158f9c541f34851a17ba2f83a0b4053&pid=Api",
                            "contentUrl": "https:\/\/nypost.com\/2019\/05\/16\/juice-wrld-on-losing-lucid-dream-money-i-really-dont-give-no-f-k\/"
                        }
                    },
                    {}
                ],
                "title": "Top "
            },
            {
                "tiles": [{
                    "query": {
                        "text": "Give Me Everything Pitbull",
                        "displayText": "Give Me Everything Pitbull",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Give%20Me%20Everything%20Pitbull&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: sandiegoreader.com",
                        "thumbnailUrl": "https:\/\/tse4.mm.bing.net\/th?id=OET.28cd1a005adf462a8e9d5c8749aaa668&pid=Api",
                        "contentUrl": "http:\/\/www.sandiegoreader.com\/events\/2015\/may\/08\/pitbull-jason-derulo-david-guetta\/"
                    }
                }, {
                    "query": {
                        "text": "All Dat Moneybagg Yo",
                        "displayText": "All Dat Moneybagg Yo",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=All%20Dat%20Moneybagg%20Yo&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: billboard.com",
                        "thumbnailUrl": "https:\/\/tse4.mm.bing.net\/th?id=OET.8f82ef309eca4762af9305e5b3c8d6ea&pid=Api",
                        "contentUrl": "https:\/\/www.billboard.com\/articles\/columns\/hip-hop\/8514529\/moneybagg-yo-interview-heartless-megan-the-stallion"
                    }
                }, {

                }, {
                    "query": {
                        "text": "Boyfriend Ariana Grande",
                        "displayText": "Boyfriend Ariana Grande",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Boyfriend%20Ariana%20Grande&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: consequenceofsound.net",
                        "thumbnailUrl": "https:\/\/tse1.mm.bing.net\/th?id=OET.7ee83af3029c42b1b5d745503b071d31&pid=Api",
                        "contentUrl": "https:\/\/consequenceofsound.net\/2019\/01\/ariana-grande-2019-tour-dates-rescheduled\/"
                    }
                }, {

                }],
                "title": "Trending"
            },
            {
                "tiles": [{
                        "query": {
                            "text": "Cuban Links Rod Wave",
                            "displayText": "Cuban Links Rod Wave",
                            "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Cuban%20Links%20Rod%20Wave&FORM=VSTREQ"
                        },
                        "image": {
                            "description": "Image from: youtube.com",
                            "thumbnailUrl": "https:\/\/tse2.mm.bing.net\/th?id=OET.e494278666d24cdc84e15d8974e5eb6b&pid=Api",
                            "contentUrl": "https:\/\/www.youtube.com\/watch?v=ISwmC4Fwfo8"
                        }
                    }, {
                        "query": {
                            "text": "Even Though I'm Leaving Luke Combs",
                            "displayText": "Even Though I'm Leaving Luke Combs",
                            "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Even%20Though%20I%27m%20Leaving%20Luke%20Combs&FORM=VSTREQ"
                        },
                        "image": {
                            "description": "Image from: flipboard.com",
                            "thumbnailUrl": "https:\/\/tse1.mm.bing.net\/th?id=OET.c1c49850dfc34e248fc3dff4dd80ca6d&pid=Api",
                            "contentUrl": "https:\/\/flipboard.com\/@abc\/hong-kong-protesters-wielding-bows-and-arrows-hunker-down-at-universities\/a-Sro9tr1gR0GHRPyUNKXceg%3Aa%3A3195386-a0792477cc%2Fgo.com"
                        }
                    }, {

                        "image": {
                            "description": "Image from: moosegazette.net",
                            "thumbnailUrl": "https:\/\/tse1.mm.bing.net\/th?id=OET.7d2861c726ff47638c01bec230bfe8b2&pid=Api",
                            "contentUrl": "https:\/\/moosegazette.net\/billie-eilish-reveals-new-single-everything-i-wanted-will-be-released-this-week\/70144\/"
                        }

                    },
                    {

                    }
                ],
                "title": "More music videos"
            }
        ]
    },
    {
        "title": "Viral videos",
        "subcategories": [{
                "tiles": [{
                    "query": {
                        "text": "The Hilarious Way Some Deer Fight",
                        "displayText": "The Hilarious Way Some Deer Fight",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=The%20Hilarious%20Way%20Some%20Deer%20Fight&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: youtube.com",
                        "thumbnailUrl": "https:\/\/tse2.mm.bing.net\/th?id=OET.ad0b72ac2aee4e709fc0a4679d7c133f&pid=Api",
                        "contentUrl": "https:\/\/www.youtube.com\/watch?v=fx4nhJnSUsQ"
                    }
                }, {
                    "query": {
                        "text": "Albino Alligator Gets A Scrub",
                        "displayText": "Albino Alligator Gets A Scrub",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Albino%20Alligator%20Gets%20A%20Scrub&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: preview.abcnews.go.com",
                        "thumbnailUrl": "https:\/\/tse1.mm.bing.net\/th?id=OET.f5c9e391c00a47c58f762686b8eba719&pid=Api",
                        "contentUrl": "https:\/\/preview.abcnews.go.com\/US\/video\/albino-alligator-scrub-67175550"
                    }
                }, {
                    "query": {
                        "text": "Hungry Californian Bear Gets Stuck In Car",
                        "displayText": "Hungry Californian Bear Gets Stuck In Car",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Hungry%20Californian%20Bear%20Gets%20Stuck%20In%20Car&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: nbclosangeles.com",
                        "thumbnailUrl": "https:\/\/tse4.mm.bing.net\/th?id=OET.f11eeb155d5249bd82d23b0a87e0fe74&pid=Api",
                        "contentUrl": "https:\/\/www.nbclosangeles.com\/news\/national-international\/"
                    }

                }],
                "title": "This week"
            },
            {
                "tiles": [{

                    "image": {
                        "description": "Image from: newslocker.com",
                        "thumbnailUrl": "https:\/\/tse4.mm.bing.net\/th?id=OET.657749b8efd94b3cb8328cc936f0d7e4&pid=Api",
                        "contentUrl": "https:\/\/www.newslocker.com\/en-uk\/news\/world_news\/clashes-rock-bolivia-amid-power-void-left-by-morales-ouster\/"
                    }
                }, {
                    "query": {
                        "text": "Deer-like Animal After 25 Years",
                        "displayText": "Deer-like Animal After 25 Years",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Deer-like%20Animal%20After%2025%20Years&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: flipboard.com",
                        "thumbnailUrl": "https:\/\/tse1.mm.bing.net\/th?id=OET.0539d0f180b94584a573c13ad4b4d3d1&pid=Api",
                        "contentUrl": "https:\/\/flipboard.com\/topic\/endangeredspecies\/vietnam-deer-rediscovered-after-nearly-30-years\/a-7xDOHyjpRaCd30Yrojer6A%3Aa%3A926749695-863c0fb353%2Fstraitstimes.com"
                    }
                }, {
                    "query": {
                        "text": "MIT Robots Enjoy A Soccer Outing",
                        "displayText": "MIT Robots Enjoy A Soccer Outing",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=MIT%20Robots%20Enjoy%20A%20Soccer%20Outing&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: abcnews.go.com",
                        "thumbnailUrl": "https:\/\/tse1.mm.bing.net\/th?id=OET.ddbb16fee5aa4601b5d914a294a571ca&pid=Api",
                        "contentUrl": "https:\/\/abcnews.go.com\/US\/mits-adorable-back-flipping-robots-enjoy-frolicking-autumn\/story?id=66853771&ref=hvper.com"
                    }
                }, {
                    "query": {
                        "text": "Here’s Why Rain Smells So Good",
                        "displayText": "Here’s Why Rain Smells So Good",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Here%E2%80%99s%20Why%20Rain%20Smells%20So%20Good&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: wusa9.com",
                        "thumbnailUrl": "https:\/\/tse1.mm.bing.net\/th?id=OET.72de2060a0c147afb265ec4807f95c84&pid=Api",
                        "contentUrl": "https:\/\/www.wusa9.com\/video\/tech\/science\/amaze-lab\/heres-why-rain-smells-so-good\/609-0c208939-ccd5-4256-b2ed-ef6c41ee58d3"
                    }
                }, {
                    "query": {
                        "text": "Dog Gets Head Stuck In Food Bag",
                        "displayText": "Dog Gets Head Stuck In Food Bag",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Dog%20Gets%20Head%20Stuck%20In%20Food%20Bag&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: youtube.com",
                        "thumbnailUrl": "https:\/\/tse3.mm.bing.net\/th?id=OET.4da45db977f2417fb421dd6d3f2c78fc&pid=Api",
                        "contentUrl": "https:\/\/www.youtube.com\/watch?v=zZke06tWOck"
                    }
                }],
                "title": "Last week"
            },
            {
                "tiles": [{
                        "query": {
                            "text": "Boxer Meets New Puppy",
                            "displayText": "Boxer Meets New Puppy",
                            "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Boxer%20Meets%20New%20Puppy&FORM=VSTREQ"
                        },
                        "image": {
                            "description": "Image from: newpetowners.com",
                            "thumbnailUrl": "https:\/\/tse2.mm.bing.net\/th?id=OET.11babe873c474598857929bb44835e9c&pid=Api",
                            "contentUrl": "http:\/\/www.newpetowners.com\/sale\/47284-Boxer.html"
                        }


                    },
                    {
                        "query": {
                            "text": "Golden Takes His Retriever Job Very Seriously",
                            "displayText": "Golden Takes His Retriever Job Very Seriously",
                            "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Golden%20Takes%20His%20Retriever%20Job%20Very%20Seriously&FORM=VSTREQ"
                        },
                        "image": {
                            "description": "Image from: tanksgoodnews.com",
                            "thumbnailUrl": "https:\/\/tse3.mm.bing.net\/th?id=OET.2aa0e5389c7348169e44ddfba91c1e00&pid=Api",
                            "headLine": "Uncovering North America's Largest Diamond",
                            "contentUrl": "https:\/\/tanksgoodnews.com\/tag\/wholesome\/page\/8\/"
                        }
                    },
                    {
                        "query": {
                            "text": "Vicious Rooster Attacks Delivery Man",
                            "displayText": "Vicious Rooster Attacks Delivery Man",
                            "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Vicious%20Rooster%20Attacks%20Delivery%20Man&FORM=VSTREQ"
                        },
                        "image": {
                            "description": "Image from: oddee.com",
                            "thumbnailUrl": "https:\/\/tse4.mm.bing.net\/th?id=OET.44a7c50f22c5494cb402f7ba3f65eaf3&pid=Api",
                            "contentUrl": "https:\/\/www.oddee.com\/miracle-mike-the-rooster-survives-vicious-farmer-attack-lives-for-months-as-sideshow-pet-49003\/"
                        }
                    }
                ],
                "title": "More viral videos"
            }
        ]
    },
    {
        "title": "TV shows",
        "subcategories": [{
                "tiles": [{
                    "query": {
                        "text": "The Flash",
                        "displayText": "The Flash",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=The%20Flash&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: screenrant.com",
                        "thumbnailUrl": "https:\/\/tse3.mm.bing.net\/th?id=OET.3f6cefd417fc4c05b9dac3bd92965cf6&pid=Api",
                        "contentUrl": "http:\/\/screenrant.com\/flash-season-4-premiere-title-filming\/"
                    }
                }, {
                    "query": {
                        "text": "Game Of Thrones",
                        "displayText": "Game Of Thrones",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Game%20Of%20Thrones&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: screenrant.com",
                        "thumbnailUrl": "https:\/\/tse2.mm.bing.net\/th?id=OET.a83fed21751b40ed9c9a5fb58ea0fee3&pid=Api",
                        "contentUrl": "https:\/\/screenrant.com\/game-thrones-rewatch-before-season-8\/"
                    }
                }, {
                    "query": {
                        "text": "Roseanne",
                        "displayText": "Roseanne",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Roseanne&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: btchflcks.com",
                        "thumbnailUrl": "https:\/\/tse3.mm.bing.net\/th?id=OET.bf8f62ae217f4ad7a7fc359c2f133f42&pid=Api",
                        "contentUrl": "http:\/\/www.btchflcks.com\/2012\/04\/reproduction-abortion-week-roseannes-discussion-of-abortion-nearly-twenty-five-years-ago-highlights-the-current-feminist-backlash.html"
                    }
                }, {
                    "query": {
                        "text": "Longmire",
                        "displayText": "Longmire",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Longmire&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: spoilertv.com",
                        "thumbnailUrl": "https:\/\/tse2.mm.bing.net\/th?id=OET.7676ec30eee84c81b68812c9f8cfb3c9&pid=Api",
                        "contentUrl": "https:\/\/www.spoilertv.com\/2016\/09\/longmire-season-5-promo-poster.html"
                    }
                }, {

                }, {
                    "query": {
                        "text": "The 100",
                        "displayText": "The 100",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=The%20100&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: denofgeek.com",
                        "thumbnailUrl": "https:\/\/tse1.mm.bing.net\/th?id=OET.59981047c1cf4fccabe1bcbe27c75aa5&pid=Api",
                        "contentUrl": "http:\/\/www.denofgeek.com\/us\/tv\/the-100\/262801\/the-100-season-5-episode-guide"
                    }
                }, {
                    "query": {
                        "text": "Supernatural",
                        "displayText": "Supernatural",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Supernatural&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: georgespigot.wordpress.com",
                        "thumbnailUrl": "https:\/\/tse2.mm.bing.net\/th?id=OET.53332136734a447d815ea0c121359c7a&pid=Api",
                        "contentUrl": "https:\/\/georgespigot.wordpress.com\/tag\/supernatural\/"
                    }
                }, {
                    "query": {
                        "text": "Better Call Saul ",
                        "displayText": "Better Call Saul ",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Better%20Call%20Saul%20&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: thecircular.org",
                        "thumbnailUrl": "https:\/\/tse3.mm.bing.net\/th?id=OET.06ba563324ef4884b779b84c4f450fa1&pid=Api",
                        "contentUrl": "http:\/\/thecircular.org\/better-watch-better-call-saul\/"
                    }
                }],
                "title": "Hot"
            }, {
                "tiles": [{
                        "query": {
                            "text": "Blue Bloods",
                            "displayText": "Blue Bloods",
                            "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Blue%20Bloods&FORM=VSTREQ"
                        },
                        "image": {
                            "description": "Image from: tvwatchus.com",
                            "thumbnailUrl": "https:\/\/tse3.mm.bing.net\/th?id=OET.9874c58cc5394cee84933646d84b3cc2&pid=Api",
                            "contentUrl": "http:\/\/www.tvwatchus.com\/rencan\/2016\/11\/13\/cbs-predictions-blue-bloods-hawaii-five-0-renewed-pure-genius-cancelled\/"
                        }


                    },
                    {


                    }
                ],
                "title": "Trending "
            },
            {
                "tiles": [{


                    },
                    {



                    }


                ],
                "title": "More TV shows"
            }
        ]
    },
    {
        "title": "Movie trailers",
        "subcategories": [{
                "tiles": [{
                        "query": {
                            "text": "Uncut Gems Trailer",
                            "displayText": "Uncut Gems Trailer",
                            "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Uncut%20Gems%20Trailer&FORM=VSTREQ"
                        }

                    }, {



                    },
                    {
                        "query": {
                            "text": "Shooting The Mafia Trailer",
                            "displayText": "Shooting The Mafia Trailer",
                            "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Shooting%20The%20Mafia%20Trailer&FORM=VSTREQ"
                        },
                        "image": {
                            "description": "Image from: impawards.com",
                            "thumbnailUrl": "https:\/\/tse2.mm.bing.net\/th?id=OET.520018245a1d47a3a8739bc447fcc461&pid=Api",
                            "contentUrl": "http:\/\/www.impawards.com\/intl\/ireland\/2019\/shooting_the_mafia.html"
                        }
                    },
                    {
                        "query": {
                            "text": "Citizen K Trailer",
                            "displayText": "Citizen K Trailer",
                            "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Citizen%20K%20Trailer&FORM=VSTREQ"
                        },
                        "image": {
                            "description": "Image from: impawards.com",
                            "thumbnailUrl": "https:\/\/tse2.mm.bing.net\/th?id=OET.20def65b3ab44346816d6cc70e5bf30a&pid=Api",
                            "contentUrl": "http:\/\/www.impawards.com\/2019\/citizen_k.html"
                        }
                    },
                    {
                        "query": {
                            "text": "Better Days Trailer",
                            "displayText": "Better Days Trailer",
                            "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Better%20Days%20Trailer&FORM=VSTREQ"
                        },
                        "image": {
                            "description": "Image from: trailers.apple.com",
                            "thumbnailUrl": "https:\/\/tse4.mm.bing.net\/th?id=OET.ce0c7ebfc81c4f66b7f455151c0cd7e5&pid=Api",
                            "contentUrl": "https:\/\/trailers.apple.com\/trailers\/independent\/better-days\/"
                        }
                    },
                    {
                        "query": {
                            "text": "Doctor Sleep Trailer",
                            "displayText": "Doctor Sleep Trailer",
                            "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Doctor%20Sleep%20Trailer&FORM=VSTREQ"
                        },
                        "image": {
                            "description": "Image from: flipboard.com",
                            "thumbnailUrl": "https:\/\/tse1.mm.bing.net\/th?id=OET.85d28a89d8ed40e792581cf66ae4dc09&pid=Api",
                            "contentUrl": "https:\/\/flipboard.com\/@SYFYWire\/wire-buzz-animaniacs-voice-cast-confirmed-for-reboot-the-turning-trailer-doct\/f-9d9cc176c7%2Fsyfy.com"
                        }


                    }
                ],
                "title": "In theaters"
            },
            {
                "tiles": [{
                    "query": {
                        "text": "Cats Trailer",
                        "displayText": "Cats Trailer",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Cats%20Trailer&FORM=VSTREQ"
                    }

                }, {
                    "query": {
                        "text": "Seberg Trailer",
                        "displayText": "Seberg Trailer",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Seberg%20Trailer&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: teaser-trailer.com",
                        "thumbnailUrl": "https:\/\/tse4.mm.bing.net\/th?id=OET.14090b5622a74f72a1ad12f6e3d64fa2&pid=Api",
                        "contentUrl": "https:\/\/teaser-trailer.com\/movie\/seberg\/"
                    }
                }, {
                    "query": {
                        "text": "A Hidden Life Trailer",
                        "displayText": "A Hidden Life Trailer",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=A%20Hidden%20Life%20Trailer&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: syko.org",
                        "thumbnailUrl": "https:\/\/tse1.mm.bing.net\/th?id=OET.b30aa4c68e6b45edb48761de399119bb&pid=Api",
                        "contentUrl": "http:\/\/www.syko.org\/2019\/08\/a-hidden-life-trailer-poster-released.html"
                    }
                }, {
                    "query": {
                        "text": "Richard Jewell Trailer",
                        "displayText": "Richard Jewell Trailer",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Richard%20Jewell%20Trailer&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: aceshowbiz.com",
                        "thumbnailUrl": "https:\/\/tse1.mm.bing.net\/th?id=OET.8da9ae36d4dc44daaf3e5ea11a816dda&pid=Api",
                        "contentUrl": "https:\/\/www.aceshowbiz.com\/movie\/richard_jewell\/photo.html"
                    }
                }, {
                    "query": {
                        "text": "Black Christmas Trailer",
                        "displayText": "Black Christmas Trailer",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Black%20Christmas%20Trailer&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: teaser-trailer.com",
                        "thumbnailUrl": "https:\/\/tse1.mm.bing.net\/th?id=OET.5d4a4e4a9c0e497c8037a3962fae6591&pid=Api",
                        "contentUrl": "https:\/\/teaser-trailer.com\/black-christmas-movie-trailer\/"
                    }
                }, {
                    "query": {
                        "text": "Jumanji: The Next Level Trailer",
                        "displayText": "Jumanji: The Next Level Trailer",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Jumanji%3A%20The%20Next%20Level%20Trailer&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: teaser-trailer.com",
                        "thumbnailUrl": "https:\/\/tse2.mm.bing.net\/th?id=OET.ff563072905d4dcfa8207088058f98fa&pid=Api",
                        "contentUrl": "https:\/\/teaser-trailer.com\/trailer-and-poster-of-jumanji-the-next-level\/"
                    }
                }, {
                    "query": {
                        "text": "Midnight Family Trailer",
                        "displayText": "Midnight Family Trailer",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Midnight%20Family%20Trailer&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: trailers.apple.com",
                        "thumbnailUrl": "https:\/\/tse1.mm.bing.net\/th?id=OET.42d52917769749a9acd813a0451e7034&pid=Api",
                        "contentUrl": "https:\/\/trailers.apple.com\/trailers\/independent\/midnight-family\/"
                    }
                }],
                "title": "Coming soon"
            },
            {
                "tiles": [{
                    "query": {
                        "text": "Queen & Slim Trailer",
                        "displayText": "Queen & Slim Trailer",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Queen%20%26%20Slim%20Trailer&FORM=VSTREQ"
                    },

                    "image": {
                        "description": "Image from: firstshowing.net",
                        "thumbnailUrl": "https:\/\/tse4.mm.bing.net\/th?id=OET.42667f22a6e2428ab95fef543eeb50d1&pid=Api",
                        "contentUrl": "https:\/\/www.firstshowing.net\/2019\/second-trailer-for-paul-feigs-holiday-romance-movie-last-christmas\/"
                    }
                }, {
                    "query": {
                        "text": "Charlie's Angels Trailer",
                        "displayText": "Charlie's Angels Trailer",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Charlie%27s%20Angels%20Trailer&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: comicbookmovie.com",
                        "thumbnailUrl": "https:\/\/tse4.mm.bing.net\/th?id=OET.2d854c6ff9a6435a9cd21830b53dfb31&pid=Api",
                        "contentUrl": "https:\/\/www.comicbookmovie.com\/action\/charlies_angels\/charlies-angels-unite-conquer-with-a-kickass-new-trailer-as-tickets-go-on-sale-plus-soundtrack-details-a171029"
                    }
                }, {
                    "query": {
                        "text": "Ford V Ferrari Trailer",
                        "displayText": "Ford V Ferrari Trailer",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Ford%20V%20Ferrari%20%20Trailer&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: filmpulse.net",
                        "thumbnailUrl": "https:\/\/tse4.mm.bing.net\/th?id=OET.7f058d4ac9e54bb4aa8a5f2b20c51461&pid=Api",
                        "contentUrl": "https:\/\/filmpulse.net\/james-mangolds-ford-v-ferrari-gets-a-new-trailer\/"
                    }
                }, {
                    "query": {
                        "text": "Waves Trailer",
                        "displayText": "Waves Trailer",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Waves%20Trailer&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: en.wikipedia.org",
                        "thumbnailUrl": "https:\/\/tse1.mm.bing.net\/th?id=OET.490beb71d3074ff7b1e85abd44744a9f&pid=Api",
                        "contentUrl": "https:\/\/en.wikipedia.org\/wiki\/Waves_(film)"
                    }
                }, {
                    "query": {
                        "text": "Midnight Traveler Trailer",
                        "displayText": "Midnight Traveler Trailer",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Midnight%20Traveler%20Trailer&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: filmaffinity.com",
                        "thumbnailUrl": "https:\/\/tse4.mm.bing.net\/th?id=OET.b3a2883c6ee64297933a42c2ac19ab4e&pid=Api",
                        "contentUrl": "https:\/\/www.filmaffinity.com\/en\/film560489.html"
                    }
                }, {
                    "query": {
                        "text": "Mickey And the Bear Trailer",
                        "displayText": "Mickey And the Bear Trailer",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Mickey%20And%20the%20Bear%20Trailer&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: impawards.com",
                        "thumbnailUrl": "https:\/\/tse4.mm.bing.net\/th?id=OET.98a80783a9944870b8f88259f4edcca1&pid=Api",
                        "contentUrl": "http:\/\/www.impawards.com\/2019\/mickey_and_the_bear.html"
                    }
                }, {
                    "query": {
                        "text": "Little Joe Trailer",
                        "displayText": "Little Joe Trailer",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Little%20Joe%20Trailer&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: santabanta.com",
                        "thumbnailUrl": "https:\/\/tse3.mm.bing.net\/th?id=OET.14aafa5049404d249fcfafafe8d99cd8&pid=Api",
                        "contentUrl": "http:\/\/www.santabanta.com\/hollywood\/134660\/little-joe-trailer-hybrid-plants-turn-evil-in-this-cannes-winner\/"
                    }
                }, {
                    "query": {
                        "text": "Portrait Of A Lady On Fire Trailer",
                        "displayText": "Portrait Of A Lady On Fire Trailer",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Portrait%20Of%20A%20Lady%20On%20Fire%20Trailer&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: wildaboutmovies.com",
                        "thumbnailUrl": "https:\/\/tse1.mm.bing.net\/th?id=OET.aac02ddfbfca402c91e040a0d10a7841&pid=Api",
                        "contentUrl": "https:\/\/www.wildaboutmovies.com\/2019_movies\/portrait-of-a-lady-on-fire\/"
                    }
                }, {
                    "query": {
                        "text": "Harriet Trailer",
                        "displayText": "Harriet Trailer",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Harriet%20Trailer&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: icecreamconvos.com",
                        "thumbnailUrl": "https:\/\/tse1.mm.bing.net\/th?id=OET.63acf90fcbc44e5baef35743c9fb724e&pid=Api",
                        "contentUrl": "https:\/\/icecreamconvos.com\/watch-harriet-trailer\/"
                    }
                }, {
                    "query": {
                        "text": "Ad Astra Trailer",
                        "displayText": "Ad Astra Trailer",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Ad%20Astra%20Trailer&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: vitalthrills.com",
                        "thumbnailUrl": "https:\/\/tse3.mm.bing.net\/th?id=OET.816ca27f02a14971b2e5c18fbba9fde6&pid=Api",
                        "contentUrl": "https:\/\/www.vitalthrills.com\/2019\/07\/18\/brad-pitt-new-ad-astra-trailer\/"
                    }
                }, {
                    "query": {
                        "text": "The Golden Glove Trailer",
                        "displayText": "The Golden Glove Trailer",
                        "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=The%20Golden%20Glove%20Trailer&FORM=VSTREQ"
                    },
                    "image": {
                        "description": "Image from: allocine.fr",
                        "thumbnailUrl": "https:\/\/tse4.mm.bing.net\/th?id=OET.5bea341b5bb140ca96c592b835ac62b2&pid=Api",
                        "contentUrl": "http:\/\/www.allocine.fr\/film\/fichefilm_gen_cfilm=250114.html"
                    }
                }],
                "title": "More movie trailers"
            }
        ]
      }
    ]
}
matrix4583
  • 31
  • 7
  • Because your json string wrong syntax, so json_decode didn't parse you json sting to array. Did you try test your json string? Let post your code to generate your json string. – Au Nguyen Nov 27 '19 at 03:35
  • @AuNguyen: the json is fine, OP didn't format the code properly, the closing `{` is left out of the code section. – catcon Nov 27 '19 at 03:58
  • What is your expected result? – catcon Nov 27 '19 at 03:58
  • I removed some json data because stackoverflow doesn't allow more than 30000 character. – matrix4583 Nov 27 '19 at 05:43
  • I re edit my question and removed the string that i don't want. The one I want are the subcategories that has all the data for title, etc...How do i add line code number to show which json i want in stackoverflow? – matrix4583 Nov 27 '19 at 05:48
  • 1
    Resolved over chat using about 5 or 6 nested foreach loops – mrwpress Nov 27 '19 at 06:56

2 Answers2

1

Per the documentation of json_decode, if you are using the second parameter "TRUE" that will return an array:

https://www.php.net/manual/en/function.json-decode.php

So, no need to typecast with (array) json_decode($response, TRUE). The following will be just fine:

$json_array = json_decode($response, TRUE);

Secondly, when I cut and paste your response you provide above into a IDE, it is broken as all get out. The response, per the documentation link I provided above, should have a structure like this:

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

Taking a JSON string (that was correctly formatted to begin with) and decoding it to an array will always look like that. Your response does not and that leads me to believe (without seeing it) that your $response is not actually properly formatted JSON.

EDIT:

See my comments in the comments below. But the bottom line here is that your trying to foreach over the JSON. There is nothing to foreach over here. It is JSON data that will be put into an array. Call out those keys without foreaching and you will be golden!

Post Chat:

The OP and I had a chat and ended up with this super nested foreach loop:

$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;
            }
        }
    }
mrwpress
  • 311
  • 1
  • 8
  • OP has post a valid JSON, he just didn't format the question properly, so the last closing `}` is left out of the code section. – catcon Nov 27 '19 at 03:51
  • I see. I miss-read and thought that response is what he was getting back from the json_decode. That is the $response. That clears things up a bit. I was confused how he got that output after the decode.... – mrwpress Nov 27 '19 at 04:24
  • However, it is pretty easy to see then that the JSON is not going to output an array to be foreach'd over. You can see in the $response code above the "bannerTiles" is: ` "bannerTiles": [{ "query": { "text": "Good As Hell Lizzo", "displayText": "Good As Hell Lizzo", "webSearchUrl": "https:\/\/www.bing.com\/videos\/search?q=Good%20As%20Hell%20Lizzo&FORM=VSTREQ" } }, { ` That doesn't even contain the array keys the OP is trying to echo out. – mrwpress Nov 27 '19 at 04:25
  • I want to get data at "subcategories": [{ "tiles": [{ } ]" after categories . Not sure what line of the code number to get from stack overflow when I posted the json output so I can't explain it clearly. – matrix4583 Nov 27 '19 at 05:54
  • echo $json_array['categories']['subcategories']['tiles']['query']['text']; – mrwpress Nov 27 '19 at 05:56
  • foreach($json_array['bannerTiles'] as $json) { echo $json_array['categories']['subcategories']['tiles']['query']['text']; } ; not working – matrix4583 Nov 27 '19 at 05:59
  • Oops. $json['categories']['subcategories']['tiles']['query']['text']; – mrwpress Nov 27 '19 at 06:01
  • can i move this discussion to chat? the 2nd code above without array is not working either..not sure if my json is formatted correctly cause i removed some of the json out put data cause of stack overflow limit the character. – matrix4583 Nov 27 '19 at 06:04
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/203148/discussion-between-mrwpress-and-matrix4583). – mrwpress Nov 27 '19 at 06:07
  • i tried your code and it still not working with all the json data. – matrix4583 Nov 27 '19 at 06:07
1

Because your json string is very troubles, you could try this code:

$str=""; // your json string
$date = date("Y/m/d");
$json_array = json_decode($str, true);
foreach($json_array['bannerTiles'] as $json)
{
    if(isset($json['webSearchUrl'])) echo "<br>Web URL: ". $json['webSearchUrl']."<br>";  
    echo "<b>"."Published on: ".$date."<br>";
    if(isset($json['displayText'])) echo "<b>Title: ".$json['displayText']."<br>";
}

foreach($json_array['categories'] as $categories_1)
{
    foreach($categories_1 as $categories_2)
    {
        foreach($categories_2 as $categories_3)
        {
            foreach($categories_3['tiles'] as $tile)
            {
                if(isset($tile['query'])) echo "<br>Web URL: ". $tile['query']['webSearchUrl']."<br>";  
                echo "<b>"."Published on: ".$date."<br>";
                if(isset($tile['query'])) echo "<b>Title: ".$tile['query']['displayText']."<br>";

                if(isset($tile['image']))
                {
                    echo "<b>"."<img class='img-responsive' src =".$tile['image'] 
                         ['thumbnailUrl']."></a><br>";          
                }
                if(isset($tile['query']))  echo $tile['query']['text'];
            }
        }
    }
}
Au Nguyen
  • 655
  • 4
  • 12