Question

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

Was it helpful?

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

OTHER TIPS

A basic loop can handle this:

foreach ($array as $movie) {
    echo $movie['name'] . ",";
}

Array_walk;

   Array_walk($array,function($key,$val){
      echo $val['name']. ', ';
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top