3

I'm trying to use an unofficial IMDb api for my project. I used this piece of code.

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

}
?>

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

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/
            }
        ]
    }
}

And this is my $array after JSON Decoding.

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

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

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.

6
  • What exactly do you want to print here? That too in which format? Please specify that first. Commented Apr 5, 2016 at 16:50
  • You should use text instead of images for this sort of data so people can easily view and work with it to help you. Also your data seems to be JSON, then use json_decode($data, true); to put it into an array Commented Apr 5, 2016 at 16:50
  • First of all... an array named array is like a dog named Dog. Please, find a meaningful name for your array, my friend. Commented Apr 5, 2016 at 16:53
  • Do you really have these indexes? Please, post a print_r of the full array so we can understand how it is organized. Commented Apr 5, 2016 at 16:54
  • This is not an Array, but JSON. And not even valid JSON. Are you receiving this directly from an API? Or you edited the answer before posting here? Commented Apr 5, 2016 at 17:28

3 Answers 3

4

From what I see, this seems like a json and not an array. In that case, you should do this first:

$array =  json_decode($array, true);

When it comes to the array traversal, you can try something like this:

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

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

2

Assuming that's JSON data that you're going to json_decode, the data, results, and titles aren't array keys, they're object properties. Try something like $array->data->results->titles[0];. The { character denotes an object property, while [ denotes an array.

Comments

2

Use

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.