Implode multidimensional array

Hi Guys

I need to implode a multidimensional array. The require output is the value separated by commas. For example, I have the following array:

    [0] => Array
            [job_type] => Administrative / Clerical Jobs

    [1] => Array
            [job_type] => Accounting / Finance jobs

    [2] => Array
            [job_type] => Architecture Jobs

    [3] => Array
            [job_type] => Art and Design Jobs


I would like to show the job_type value separated by a comma.


Administrative / Clerical Jobs, Accounting / Finance jobs, Architecture Jobs, Art and Design Jobs

This is possibly an amateurish solution, but it does what you want


$array = array(array('job_type'=>'Administrative / Clerical Jobs'),
        array('job_type' => 'Accounting / Finance jobs'),
        array('job_type' => 'Architecture Jobs'),
        array('job_type' => 'Art and Design Jobs'));

echo '<pre>';
echo '</pre>';

foreach($array as $val){
       $t .= $val['job_type'].',';        
print substr ( $t , 0 , strlen($t) -1 );

Thanks, but was looking for something a little cleaner :slight_smile: Any ideas how to do it using array_map?

array_map returns another array - so that won’t put you any further forward.

I don’t know (yet) if there’s anything cleaner than my first effort??

Wouldn’t using array_map present this

function GetJobType($e)
    return $e['job_type'];

echo implode(', ', array_map('GetJobType', $array));

Yes, that works, but is it cleaner? Not sure what people mean when they say ‘cleaner’ :0(

Why implode? Why not [fphp]serialize[/fphp] or [fphp]json_encode[/fphp]? Implode isn’t the only way to get a string representation of an array that can be converted back to an array.

[ot]PHP 5.5.0 might get a new function, array_column(), that would make life easier here:

echo implode(', ', array_column($array, 'job_type'));


Who said anything about converting the string back to an array?

Off Topic:

Yes, indeed, though he might be simplifying the example some here. In any event, if those strings contain commas (and they’re free form it seems so they could) implode isn’t the right tool, even with array_column

Who said anything about converting the string back to an array?

That more often going to be the case than not. He hasn’t mentioned why he’s doing this, so I have no point of reference here.

array_column doesn’t provide a comma delimited output either, so at best you could replace array_map with array_column, but you can’t remove implode…unless I am missing something.

My take was that he just wants to print the strings separated by some commas. As you say, he hasn’t mentioned why he’s doing this so why second guess? At the very least, let’s ask Zaggs about his other requirements, if any, before jumping onto the this needs to be reversible, or not-only-comma separated, horse!

Who said anything about removing array_column()? (I’m seeing a pattern emerging! :beer:)

On the contrary, I like array_column, that is what I was originally searching for when I looked through the php docs to begin with, but when I didn’t find it, I resorted to array_map. What I meant to imply is you can use array_column and array_map interchangeably in this endeavor, but implode() to my knowledge is the only way to take an array and turn it into a delimited string (apart from coding a loop).

Off Topic:

It’s been a long day, my brain has started to shut down…