문제

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

도움이 되었습니까?

해결책

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

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

    echo implode(',', $actors);

다른 팁

A basic loop can handle this:

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

Array_walk;

   Array_walk($array,function($key,$val){
      echo $val['name']. ', ';
    });
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top