Checking if the array pointer has reached the last item in an array?

Here i have a simple while loop to achieve this result:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 <— This is echoed out to the browser. Notice there is no comma after 10.

<?php
     for ($count = 0; $count <= 10; $count++) {
          echo $count;
          if ($count == 10) {
               break;
          }
          echo ', ';
     }
?>

I want to achieve the same thing with ages.

I have a simple array here

<?php
$ages = array(2, 3, 5, 8, 12, 14, 18);
?>

I want the ages echoed out like so:

2, 3, 5, 8, 12, 14, 18 <---- Without the comma’s too

Here’s my code:

<?php
     while ($age = current($ages)) {
          echo $age;
          // How do i know if the array pointer has reached the end
          // So that i can stop it from putting the comma below this line..
          echo ', ';
          next($ages);
     }
?>

How do i know if the array pointer has reached the end? So that i can stop it from putting the comma…

You can do exactly as you did in the first loop:

for ($i = 0; $i < count($ages); $i++) {
  echo $ages[$i];
  if ($i = count($ages) - 1) {
    break;
  }
  echo ', ';
}

But there’s an easier way

echo implode(', ', $ages);

Thanks Dan…

Worked a charm…sorry if it’s a stupid question…can this be done in a while loop…just for learning purposes…i know there might be no point doing it in a while loop, but can it be done…

Theoretically yes, but you need a counter still…it’s basically same idea as for loop


$counter = 0;
while($counter != count($ages))
{
  echo $ages[$counter];
  if ($counter == count($ages) - 1) {
    break;
  }
  echo ', ';
  $counter++;
}

I wonder if there’s any better ways of doing it with while…

Thanks Kefeso…

Using your original code, next($ages) would’ve returned FALSE on the call at the end of the array, which you could use as a check to skip printing the comma. Just make sure your condition does strict comparison.

if (next($ages) === false) ...

You can try this for loop… for learning purposes:


$ages = array(2, 3, 5, 8, 12, 14, 18);
$count = count($ages);

for ($i = 0; $i < $count; $i++) {
	echo $ages[$i];
	if ( end($ages) != $ages[$i] ) {
  		echo ', ';
  	}
}

That will end the loop prematurely if the last element in the array appears anywhere earlier in the array as well.

let’s make this simpler by displaying the first term before the loop


$ages = array(2, 3, 5, 8, 12, 14, 18); 

echo current($ages);
while ($age = next($ages)) {
      echo ', ' . $age;
} 

reset() is necessary for such ancient techniques :slight_smile:

Sorry mate, looks like you’re wrong on this one. The documentation says that it’s already in the right place.

reset is only necessary when you want to rewind back to the start.


$array = array('step one', 'step two', 'step three', 'step four');

// by default, the pointer is on the first element
echo current($array) . "<br />\
"; // "step one"

// skip two steps
next($array);
next($array);
echo current($array) . "<br />\
"; // "step three"

// reset pointer, start again on step one
reset($array);
echo current($array) . "<br />\
"; // "step one"

by default

here is the problem. You think that the pointer is on the first element. But you can be wrong.
It is much like variable initializing.
As you should always do $i=0 before increment it, as reset() is needed for these functions before use.

Let’s go back to the documentation instead of relying on here’say.

Every array has an internal pointer to its “current” element, which is initialized to the first element inserted into the array.

It’s only when the instantiation of the array and the usage of the array pointers are separated from each other, that reset becomes useful to use as a safety precaution, in case the pointer has been shifted during the interim.

As it’s previously told this is not the best case where to use while loop, so i suggest to more use the for loop


$ages = array(31, 12, 32, 43, 65, 43);
for ($i = 0; $i < count($ages); $i++) {
    $value = ($i != count($ages) - 1) ? ',' : '';
    echo $ages[$i] . $value;
}

Please explain?

It’s only when

Here is the problem. Countless bugs made from such assumptions.
“I won’t check/initialize because it’s just unnecessary” :slight_smile:
But I won’t press on it. It comes only with some experience. Negative experience :slight_smile:

I don’t blame you for being twitchy, so please explain how the following code can result in anything else but the expected result?


$ages = array(2, 3, 5, 8, 12, 14, 18); 
echo current($ages);
// expected: 2

Are there any conditions, any at all, that cause the above code to fail its expected results, where a reset would have saved us.

To follow up, when the possibility of shenanigans can occur between the array being created, and current or next being used on it, then is makes very good sense to issue a reset as a safety precaution.

I think that I would be right to presume that Shrapnel_N5 always add reset, as it causes no harm and has resolved complicated coding problems that he has experienced in the past.

How closely though does that technique relate to this:


$str = "Hello world!";
if ($str > "") { // guard condition
    // do stuff with $str
};

$ages = array(2, 3, 5, 8, 12, 14, 18); 
reset($ages); // ???
echo current($ages);

Is the above if condition more or less useful than issuing a reset on a newly created array?

We may have a difference of opinion here, because I currently consider the reset to be less useful than the guard condition.