1

I have an array $skills. I want to make a comma seperated list of all the skills.

I thought I could do this with array implode, but it looks like I'll have to use a foreach() and append to a string. Is there a way to do this with just array implode?

[skills] => Array
        (
            [0] => Array
                (
                    [email] => don.pinkus@gmail.com
                    [skill] => Statistics
                )

            [1] => Array
                (
                    [email] => don.pinkus@gmail.com
                    [skill] => Adobe Creative Suite
                )

            [2] => Array
                (
                    [email] => don.pinkus@gmail.com
                    [skill] => HTML + CSS
                )

            [3] => Array
                (
                    [email] => don.pinkus@gmail.com
                    [skill] => Web Analytics
                )
Don P
  • 60,113
  • 114
  • 300
  • 432

2 Answers2

3

how about:

implode(",", array_column($skills, 'skill'));
Lyn Headley
  • 11,368
  • 3
  • 33
  • 35
0

For PHP 5.5, you can use Lyn's solution. For versions before that (5.3 and 5.4):

implode(', ', array_map(function($a) {return $a['skill'];}, $skills));

Breaking it down into multiple lines to make it a little easier to read:

$array = array_map(
    function($a) {
        return $a['skill'];
    }, $skills);
echo implode(', ', $array);

Here's a solution for before PHP 5.3 (and probably the most efficient):

$out = '';
foreach ($skills as $val){
    $out .= $val['skill'].', ';
}
echo rtrim($out, ', ');
Expedito
  • 7,771
  • 5
  • 30
  • 43