I have this array loop:
foreach ( $event_entrance as $event_entrance_s ) {
_e($event_entrance_s,'holidayge');
echo ', ';
}
I'd like to ger rid of comma
at the end of the last loop.
Any ideas? Seems simple, but it isn't for me :)
What about...
$limit = count($event_entrance);
foreach ($event_entrance as $key => $event_entrance_s) {
_e($event_entrance_s,'holidayge');
if ($key < ($limit-1)) {
echo ', ';
}
}
As long as your keys are integers and sequential, this should work exactly as you intend. If you're using integers, but they are not in any particular order, putting this before your foreach()
loop will fix that:
$event_entrance = array_values($event_entrance);
If you are using strings as keys instead of integers, try something like this:
$limit = count($event_entrance);
$i = 1;
foreach ($event_entrance as $event_entrance_s) {
_e($event_entrance_s,'holidayge');
if ($i < $limit) {
echo ', ';
}
++$i;
}
Two options:
implode
to put things together, it handles this edge case for you easily. Seriously, implode is greatTry something like this:
$event_entrance_count = count($event_entrance);
$loop_number = 1;
foreach ( $event_entrance as $event_entrance_s ) {
_e($event_entrance_s,'holidayge');
if(!$loop_number == $event_entrance_count) {
echo ', ';
}
$loop_number++;
}
$fn = function($v) { return _e($v,'holidayge'); };
$arr = array_map($fn, $event_entrance );
echo implode(',', $arr);