Hello all,
I have an array like this:
PHP Code:
$people = array('Alan', 'Steve', 'John', 'Darren');
I was just wondering how I would be able to format that so it looked like this:
"Alan, Steve, John and Darren"
or if the array was this
PHP Code:
$people = array('Alan', 'Steve');
it would output
"Alan and Steve"
So I wrote some code that would do this which is this:
PHP Code:
<?php
$people = array('Alan', 'Steve', 'John', 'Darren');
$final_string = '';
for($i=0; $i < count($people); ++$i)
{
if($i === count($people) - 1)
{
$final_string .= " and " $people[$i];
}
else
{
if($i === 0)
{
$final_string .= $people[$i];
}
else
{
$final_string .= ", " . $people[$i];
}
}
}
?>
I'm just wondering if there is a better way to do this. Any help is appreciated, thank you for your time
- Ben