PHP - 从数组中的键获取动态值并放入列表中

PHP  - 从数组中的键获取动态值并放入列表中

问题描述:

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

我正在制作一个电影评论网站,但我遇到了阵列问题。 如何从一组编写器中获取多个值? p>

我从API 数组([0] =>数组([nconst] =&gt]得到这样的响应 ; nm0604555 [name] => Chris Morgan [attr] =>(写自))[1] =>数组([nconst] => nm0860155 [name] => Gary Scott Thompson [attr] => ;(字符))) code> p>

我试过 echo $ array [0] ['name']; code>但它只给了我第一个值 Chris Morgan code>因为 [0] code> p>

数组响应键也会因电影中的演员数量而异。

那么我该怎样做才能列出列表中的作家? 像这样: Chris Morgan,Gary Scott Thompson code> p> div>

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']. ', ';
    });