a coder
a coder

Reputation: 7649

Cascading implode on an array

I'm looping through a set of data and pulling the following results:

$str[] = $id . '~' . $desc;

At the end of the loop I put these together:

$txt_str = implode('^',$str);

echo $txt_str;

1~one^2~two^3~three

Out of curiosity, could I get the same result by directly processing an array?

$arr[] = array('id'=$id, 'desc'=>$desc);

So something like:

$txt_str = makemeasammich('~', '^', $arr);

echo $txt_str;

1~one^2~two^3~three

Is there a native PHP function that has this capability?

Upvotes: 0

Views: 179

Answers (1)

Florent
Florent

Reputation: 12420

There is no such native PHP function.
Use implode() and array_map() (with anonymous function):

implode('^', array_map(function($value) {
    return implode('~', $value);
}, $myArray));

Upvotes: 1

Related Questions