Traversing array and separating by commas [duplicate]

-4

I have an array, and with var_dump it returns this:

array (size=2)
  0 => 
    array (size=2)
      'id' => int 18
      'name' => string 'Drama' (length=5)
  1 => 
    array (size=2)
      'id' => int 10765
      'name' => string 'Sci-Fi & Fantasy' (length=16)

In what way, can I just get the 'name' from the list and sort by commas? In this example, I wanted it to be: Drama, Sci-Fi & Fantasy

    
asked by anonymous 30.07.2018 / 05:05

1 answer

0

You can do this:

// array recebido
$array = array(

    array("id"=> 18, "name" => "Drama"),
    array("id"=> 10765, "name" => "Sci-Fi & Fantasy")

);

// string de saída
$string = "";

foreach($array as $valor){
    // adiciona o valor
    $string .= $valor['name'].", ";
}

// retira a ultima vírgula e o espaço
$string = substr($string, 0, -2);

// mostra o resultado
echo $string;
    
30.07.2018 / 05:25