Concactenating an array
Posted: Sun Aug 09, 2009 9:46 am
Let's say I've got an array as follows:
And I want to join them into a single string, using a hyphen as the glue. I know I can do the following:
But what if all I want to glue together are the first two elements? That is to say, so I end up with
bananaboat-frederick
as opposed to
bananaboat-frederick-lambda
Is there an elegant way of achieving this? My current solution involves a function that loops iterates through the array x times and returns the desired result, however I can't help but think it is somewhat clunky.
Code: Select all
Array
(
[0] => bananaboat
[1] => frederick
[2] => lambda
)Code: Select all
implode('-', $myArray);bananaboat-frederick
as opposed to
bananaboat-frederick-lambda
Is there an elegant way of achieving this? My current solution involves a function that loops iterates through the array x times and returns the desired result, however I can't help but think it is somewhat clunky.