I have this array:
array (size=5)
35 => string '3' (length=1)
24 => string '6' (length=1)
72 => string '1' (length=1)
16 => string '5' (length=1)
81 => string '2' (length=1)
I want to implode id to get:
$str = '35-3|24-6|72-1|16-5|81-2';
How to get it the easy way?
Thanks.
Solution
You can do it cleanly by joining strings, while using eg. custom associative mapping function, which looks like that:
Full example and test
The full solution using it could look like that:
and the result is:
which matches what you expected.
The proof is here: http://ideone.com/HPsVO6
You
cannotcan do this using implode, see @havelock's answer below, however it would be easier to use a loop or another form of iteration.One possibility would be like this
I haven't tested this, but it should be pretty straight forward ...