I’m trying to create a custom function to take a array and add comma. Like for a location or list of items.
function arraylist($params)
{
$paramlist = reset($params);
while($item = next($params))
{
$paramlist = $item . ', ' . $paramlist;
}
return $paramlist;
}
$location = array('San Francisco','California','United States');
echo arraylist($location);
San Francisco , California, United States is the output. It should out put San Francisco, California, United States
This is a duplicate of the function
implodealready present in PHP, is there a reason you do this by hand?The above does the same as your
arraylist-function.Small update: I noticed you append your ‘next item’ to the beginning of your string (
$item . ', ' . $paramlist), which will inverse your array order. The output will be (United States, California, San Francisco). If this is on purpose, please usearray_reverseto achieve the same ordering (together withimplode).