Skip to content
Advertisement

Implode array with array of glue strings

I have a awkward need but I need to interleave an array with another array before imploding the result. I guess my better option would be less talk more example

Array number one

[0] => "John has a ", [1] => "and a", [2] => "!" 

Array number two

[0] => 'Slingshot", [1] => "Potato"

I need to produce

John has a Slingshot and a Potato!

My question is can I do that with an implode or I must build my own function?

Advertisement

Answer

Adapted from comment above.

Are you sure you don’t just want string formatting?

echo vsprintf("John has a %s and a %s!", array('slingshot', 'potato'));

Output:

John has a slingshot and a potato!
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement