Implode array values?

So I have an array like this:

Array ( [0] => Array ( [name] => Something ) [1] => Array ( [name] => Something else ) [2] => Array ( [name] => Something else.... ) ) 

Is there an easy way to insert values ​​into a string, for example:

 echo implode(', ', $array[index]['name']) // result: Something, Something else, Something else... 

without , using a loop to instantiate values, for example:

 foreach ($array as $key => $val) { $string .= ', ' . $val; } $string = substr($string, 0, -2); // Needed to cut of the last ', ' 
+7
source share
3 answers

The simplest way is when you have only one element in internal arrays:

 $values = array_map('array_pop', $array); $imploded = implode(',', $values); 
+21
source

You can use the general trick of array_map() to “smooth out” a multidimensional array, then implode() result of a “flattened” one, but inside PHP, it still loops through your array when you call array_map() .

 function get_name($i) { return $i['name']; } echo implode(', ', array_map('get_name', $array)); 
+15
source

In PHP 5> = 5.5.0

 implode(', ', array_column($array, 'name')) 
+7
source

All Articles