Andrew
Andrew

Reputation: 238937

How to use a foreach loop, but do something different on the last iteration?

This is probably a simple question, but how do you iterate through an array, doing something to each one, until the last one and do something different?

I have an array of names. I want to output the list of names separated by commas.

Joe, Bob, Foobar

I don't want a comma at the end of the last name in the array, nor if there is only one value in the array (or none!).

Update: I can't use implode() because I have an array of User model objects where I get the name from each object.

$users = array();
$users[] = new User();

foreach ($users as $user) {
    echo $user->name;
    echo ', ';
}

How can I achieve this and still use these objects?

Update: I was worrying too much about how many lines of code I was putting in my view script, so I decided to create a view helper instead. Here's what I ended up with:

$array = array();
foreach($users as $user) {
    $array[] = $user->name;
}
$names = implode(', ', $array);

Upvotes: 6

Views: 2671

Answers (8)

bng44270
bng44270

Reputation: 349

I actually find it easier to create my comma delimited text a little differently. It's a bit more wordy, but it's less function calls.

<?php

$nameText = '';
for ($i = 0; $i < count($nameArray); $i++) {
    if ($i === 0) {
        $nameText = $nameArray[$i];
    } else {
        $nameText .= ',' . $nameArray[$i];
    }
}

It adds the comma as a prefix to every name except where it's the first element if the array. I have grown fond of using for as opposed to foreach since I have easy access to the current index and therefore adjacent elements of an array. You could use foreach like so:

<?php

$nameText = '';
$nameCounter = 0;
foreach ($nameArray as $thisName) {
    if ($nameCounter === 0) {
        $nameText = $thisName;
        $nameCounter++;
    } else {
        $nameText .= ',' . $thisName;
    }
}

Upvotes: 0

raveren
raveren

Reputation: 18541

I personally found the fastest way (if you're into micro optimization) is:

  if(isset($names[1])) {
     foreach ($names as $name) {
       $result .= $name . ', ';
    }
    $result = substr($result, 0, -2);
  } else {
    $result = $names[0];
  }

isset($names[1]) is the fastest (albeit not so clear) way of checking the length of an array (or string). In this case, checking for at least two elements is performed.

Upvotes: 0

James Anderson
James Anderson

Reputation: 27478

I come accross this a lot building SQL statements etc.

$joiner = " ";
foreach ($things as $thing) {
    echo "  $joiner $thing \n";
    $joiner = ',';
}

FOr some reason its easier to work out the logic if you think of the ",", "AND" or "OR" as an option/attribute that goes before an item. The problem then becomes how to suppress the the "," on the first line.

Upvotes: 1

Doug Neiner
Doug Neiner

Reputation: 66201

Use implode:

$names = array('Joe', 'Bob', 'Foobar');
echo implode(', ', $names); # prints: Joe, Bob, Foobar

To clarify, if there is only one object in the array, the ', ' separator will not be used at all, and a string containing the single item would be returned.

EDIT: If you have an array of objects, and you wanted to do it in a way other than a for loop with tests, you could do this:

function get_name($u){ return $u->name; };
echo implode(', ', array_map('get_name', $users) ); # prints: Joe, Bob, Foobar

Upvotes: 15

dar7yl
dar7yl

Reputation: 3757

Sometimes you might not want to use implode. The trick then is to use an auxiliary variable to monitor not the last, but the first time through the loop. vis:

$names = array('Joe', 'Bob', 'Foobar');
$first = true;
$result = '';
foreach ($names as $name)
{
   if (!$first)
      $result .= ', ';
   else
      $first = false;

   $result .= $name;
}

Upvotes: 3

Hassan Syed
Hassan Syed

Reputation: 20495

psuedocode....

integer sigh=container.getsize();
sigh--;
integer gosh=0;
foreach element in container
{
  if(gosh!=sigh)
    dosomething();
  else 
    doLastElementStuff();
  gosh++;
}

looking at all the other answers, it seems PHP has gotten a lot more syntactic S since I last wrote anything in it :D

Upvotes: 1

leepowers
leepowers

Reputation: 38318

implode(', ', $array_of_names)

Upvotes: 1

&#181;Bio
&#181;Bio

Reputation: 10758

$array = array('joe', 'bob', 'Foobar');
$comma_separated = join(",", $array);

output:

joe,bob,Foobar

Upvotes: 6

Related Questions