sgalonska wrote:Hello,
can anybody tell me how I can find out inside a foreach if my current value is the last value? Especially when iterating over associative arrays with text keys.
Would be great ;-).
thx
Sebastian
Associative arrays will be trickey, I don't think you can reference too them via an ID for the reference so what I'm about to tell you will not work for them.
If you are using a foreach statement all you need to do is keep a cunter variable within the loop, incrimenting with each pass of the loop like so:
<?php
$SomeArray = array('q', 'w', 'e', 'r', 't', 'y');
$Counter = 0;
echo ' ';
foreach ($SomeArray as $Element)
{
echo $SomeArray[$Counter];
++$Counter;
}
?>
As for associative arrays you'll need to copy the array into an indexed array for it to work 🙁
Live example of above code: here, here (IncID's)
Edit: this may also be of use.
Cheers,
Ryan Jones