Search code examples
phparraysapiimdb

PHP - Get dynamic values from keys from array and put into a list


I am working on a movie review site but I am stuck at an array problem. How can I get multiple values from an Array of writers?

I get a response like this from the API Array ( [0] => Array ( [nconst] => nm0604555 [name] => Chris Morgan [attr] => (written by) ) [1] => Array ( [nconst] => nm0860155 [name] => Gary Scott Thompson [attr] => (characters) ) )

I tried echo $array[0]['name']; but it only gives me the first value Chris Morgan because of the [0]

Also the array response keys varies depending on how many actors are in a film.

So what can I do to list the Writers in a list? Like this: Chris Morgan, Gary Scott Thompson


Solution

  • With my solution, you will not have a , after the last element :

     $actors= array();
        foreach ($array as $movie) {
            $actors[] = $movie['name'];
        }
    
        echo implode(',', $actors);