PHP foreach loop in nested array
<p>I'm trying to use an unofficial IMDb api for my project. I used this piece of code.</p>

<pre><code>&lt;form name="search-imdb" autocomplete="off" method="post" action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;"&gt;
&lt;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" /&gt;
&lt;input type="submit" name="submit" title="Click to Fetch Data from IMDb" value="Search IMDb" /&gt;
&lt;/form&gt;

&lt;?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 =&gt; $data){
    if(is_array($data)){
        foreach($data as $subkey =&gt; $results){
            if(is_array($results)){
                foreach($results as $key =&gt; $titles){
                    echo $titles."&lt;br /&gt;";
                }
            } else {
                echo $results."&lt;br /&gt;";
            }
        }
    } else {
        echo $data."&lt;br /&gt;";
    }
}
    curl_close($ch);
} else {

}
?&gt;
</code></pre>

<p><strong>I need to get the values of the "titles" array ONLY. (title, id, url)</strong> This was the JSON code.</p>

<pre><code>Array{
"status": "success",
"code": 200,
"message": "ok",
"term": "jQuery21409989625962002346_1459839763205&amp;q",
"search_url":"http://www.imdb.com/find?q=batmansuperman&amp;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/
            }
        ]
    }
}
</code></pre>

<p>And this is my $array after JSON Decoding.</p>

<pre><code>Array ( [status] =&gt; success [code] =&gt; 200 [message] =&gt; ok [term] =&gt; megamind [search_url] =&gt; http://www.imdb.com/find?q=megamind&amp;s=all5703f4f88841f [data] =&gt; Array ( [results] =&gt; Array ( [titles] =&gt; Array ( [0] =&gt; Array ( [title] =&gt; Megamind [id] =&gt; tt1001526 [url] =&gt; http://www.imdb.com/title/tt1001526/?ref_=fn_al_tt_1 ) [1] =&gt; Array ( [title] =&gt; Megamind [id] =&gt; tt1785464 [url] =&gt; http://www.imdb.com/title/tt1785464/?ref_=fn_al_tt_2 ) [2] =&gt; Array ( [title] =&gt; MegaMind [id] =&gt; tt1890468 [url] =&gt; http://www.imdb.com/title/tt1890468/?ref_=fn_al_tt_3 ) [3] =&gt; Array ( [title] =&gt; Mega Mindy [id] =&gt; tt0891395 [url] =&gt; http://www.imdb.com/title/tt0891395/?ref_=fn_al_tt_4 ) [4] =&gt; Array ( [title] =&gt; Megamind [id] =&gt; tt3022564 [url] =&gt; http://www.imdb.com/title/tt3022564/?ref_=fn_al_tt_5 ) [5] =&gt; Array ( [title] =&gt; Megamind: The Button of Doom [id] =&gt; tt1847645 [url] =&gt; http://www.imdb.com/title/tt1847645/?ref_=fn_al_tt_6 ) [6] =&gt; Array ( [title] =&gt; Megamind [id] =&gt; tt3624292 [url] =&gt; http://www.imdb.com/title/tt3624292/?ref_=fn_al_tt_7 ) [7] =&gt; Array ( [title] =&gt; Megamind [id] =&gt; tt2173285 [url] =&gt; http://www.imdb.com/title/tt2173285/?ref_=fn_al_tt_8 ) [8] =&gt; Array ( [title] =&gt; Mega Mindy Versus ROX [id] =&gt; tt4706602 [url] =&gt; http://www.imdb.com/title/tt4706602/?ref_=fn_al_tt_9 ) [9] =&gt; Array ( [title] =&gt; Aa Megamisama [id] =&gt; tt0872301 [url] =&gt; http://www.imdb.com/title/tt0872301/?ref_=fn_al_tt_10 ) ) [characters] =&gt; Array ( [0] =&gt; Array ( [title] =&gt; Megamind [id] =&gt; ch0194198 [url] =&gt; http://www.imdb.com/character/ch0194198/?ref_=fn_al_ch_1 ) [1] =&gt; Array ( [title] =&gt; Megamind's Mother [id] =&gt; ch0229958 [url] =&gt; http://www.imdb.com/character/ch0229958/?ref_=fn_al_ch_2 ) [2] =&gt; Array ( [title] =&gt; Megamind's Father [id] =&gt; ch0229956 [url] =&gt; http://www.imdb.com/character/ch0229956/?ref_=fn_al_ch_3 ) ) [names] =&gt; Array ( [0] =&gt; Array ( [title] =&gt; Megamind [id] =&gt; nm6292338 [url] =&gt; http://www.imdb.com/name/nm6292338/?ref_=fn_al_nm_1 ) ) [keywords] =&gt; Array ( [0] =&gt; Array ( [title] =&gt; megami [id] =&gt; megami [url] =&gt; http://www.imdb.com/keyword/megami/?ref_=fn_al_kw_1 ) ) [companies] =&gt; Array ( [0] =&gt; Array ( [title] =&gt; Megami [id] =&gt; co0544394 [url] =&gt; http://www.imdb.com/company/co0544394/?ref_=fn_al_co_1 ) [1] =&gt; Array ( [title] =&gt; Mega Mind Media [id] =&gt; co0558438 [url] =&gt; http://www.imdb.com/company/co0558438/?ref_=fn_al_co_2 ) [2] =&gt; Array ( [title] =&gt; Elm Tree Gaming [id] =&gt; co0202064 [url] =&gt; http://www.imdb.com/company/co0202064/?ref_=fn_al_co_3 ) ) ) ) )
</code></pre>

<p>Can anyone tell me what I'm doing wrong here? I'm getting this error - "Array to String Conversion"</p>

<p>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.</p>