1

I have an array of items:

array(
    'apples',
    'oranges',
    'pineapples'
)

that I would like to format like so: "Some of my favorite fruits are apples, oranges and pineapples"

So, using PHP I would like to be able to transform that array into a reader friendly apples, oranges and pineapples.

At first I thought I might be able to use array_map but i'm not sure how I would go about telling what the last and 2nd to last items are (it's easy to add a , after each item with array_map, however you wouldn't want a comma before the last item)

Thoughts?

0

3 Answers 3

4

Divide the problem to conquer it: Take out the last element, create the comma separated list and then add the last element.

$array = array(
    'apples',
    'oranges',
    'pineapples'
);

$last = array_pop($array);
echo implode(', ', $array), ' and ', $last; # apples, oranges and pineapples

Naturally this only makes sense when there are at least two values in the array. Functions used: implodeDocs (probably more suitable than array_map in this case) and array_popDocs.


On the other hand, if the values do not contain any , you can first create a comma separated string with the implode function and then use a regular expression to replace the last comma with and.

That done will also work on arrays that have less than 2 elements. I do this in a loop that removes one element from the array in each step so that it shows how it behaves:

$array = array(
    'apples',
    'oranges',
    'pineapples'
);

foreach($array as $v)
{
    echo preg_replace('(,([^,]*)$)', ' and$1', implode(', ', $array)), "\n";
    array_pop($array);
}

Output:

apples, oranges and pineapples
apples and oranges
apples
Sign up to request clarification or add additional context in comments.

Comments

2

Ok, so as others have commented implode is a solution.

I would probably end up using a combination of implode, strripos and substr_replace:

$string = implode(', ', $fruit); 
$pos = strripos($string, ', ');
echo substr_replace($string, ' and ', $pos, 2);

which results in apples, oranges and pineapples.

I'd gladly post a more elegant solution if I could think of one, but this is a quick and easy way to get at the change you need.

1 Comment

I concede that the one @hakre posted using "array_pop" is a cleaner way of doing it!
1

You can know which one is the last element by counting them:

$array = array(
    'apples',
    'oranges',
    'pineapples'
);

$count= count($array);
// because the key start from 0 , we must subtract 1 
$lastKey= $count-1;


/// now u can do that
echo "<br>1st :".$array[0];
echo "<br>2nd :".$array[1];
echo "<br>3rd :".$array[2];
echo "<br>last:".$array[$lastKey];

// or same as u like

foreach($array as $key=>$value){
    echo $value;
    if($key == $lastKey){
        echo ".";
    }elseif($key == ($lastKey-1)){
        echo " and ";
    }else{
        echo " , ";
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.