View Full Version : Knowing when foreach ends
Dabrowski
11-27-04, 09:47 AM
Is there a way to determine when a foreach is empty and returning a different string? Other than counting the array and using an increment variable?
I would assume that the foreach loop goes through the iterations until the $value is empty, in which case you could check for it and return a different string. But it doesn't work. I've tried !$value, !isset($value), empty($value), but nothing works. I've even tried putting it before and after the echo $value line.
<?
$val = array("Mike", "Bob", "Charlie", "John", "Richard");
foreach ($val as $value) {
echo $value;
if (!$value) {
$punct = ".";
} else {
$punct = ", ";
}
echo $punct;
}
?>
I would assume that the foreach loop goes through the iterations until the $value is empty . . .
You will not get an empty value iteration at the end. It will go though once for each value - just like it says - foreach. It does not say foreach +1.
Dabrowski
11-27-04, 05:29 PM
You will not get an empty value iteration at the end. It will go though once for each value - just like it says - foreach. It does not say foreach +1.
Oh. So then the only choice would be to use an increment value?
$val = array("Mike", "Bob", "Charlie", "John", "Richard");
$i=0;
$number=count($val);
foreach ($val as $value) {
echo $value;
if ($i == $number) {
echo ".";
} else {
echo ", ";
}
$i++;
}
Robert Plank
11-27-04, 08:12 PM
Is the foreach code you've shown just an example, or is that showing something you're really trying to do?
If all you want is to show an array in the form: "one, two, three." ... just do:
$val = array("Mike", "Bob", "Charlie", "John", "Richard");
echo implode(", ", $val) . ".";
riskynil
11-27-04, 10:35 PM
I like the implode method myself, but I'm not sure I understand why you want to check for an empty value in the first place. If you only want to put the period at the end of the loop, it seems like it should be outside of the loop in the first place.
<?
$val = array("Mike", "Bob", "Charlie", "John", "Richard");
foreach ($val as $value) {
echo $value.', ';
}
echo '.';
?>
Like that. If you don't want it to display the period if there's nothing in the array, an if statement (again, outside of the loop!) seems the way to go:
<?
if (count($val) > 0)
echo '.';
?>
Happy trails!
-- Ryan
Dabrowski
11-28-04, 09:00 AM
I like the implode method myself, but I'm not sure I understand why you want to check for an empty value in the first place. If you only want to put the period at the end of the loop, it seems like it should be outside of the loop in the first place.
<?
$val = array("Mike", "Bob", "Charlie", "John", "Richard");
foreach ($val as $value) {
echo $value.', ';
}
echo '.';
?>
Ryan, I just didn't want to have a ", " after the last element.
Thanks Robert. I wasn't really familiar with implode, but that seems like the ticket. I wish there was a website that would email one or two PHP functions a day. There are so many that we just never come in contact with.
riskynil
11-28-04, 01:30 PM
Ryan, I just didn't want to have a ", " after the last element.
Doah! I forgot about that! Well, the implode method still works perfectly for such an example. =)
-- Ryan
vBulletin v3.6.0, Copyright ©2000-2010, Jelsoft Enterprises Ltd.