View Full Version : Array remaining size from an index

09-14-2009, 12:27 PM
Alright so I have this kind of a situation I was hoping someone could
help me out. I havent done much with Arrays, so this might sound trivial.

I have an array for example 10 in size $size=count($array).

But then in a loop while im at index (for example) 4, i would like to get
a variable out which has stored in it the remaining size from this index
to the end of the array. ie (or an alternative solution is as great).
"Im at index 4 now out of 10, There are 6 more to go".
"Im at index 5 now out of 10, There are 5 more to go".
"Im at index 6 now out of 10, There are 4 more to go".
and so on..


09-14-2009, 01:13 PM
$size = count($array);
for ($i = 0; $i < $size; ++$i)
$iCurIndex = $i + 1;
printf('I\'m at index %d now out of %d. There are %d to go.' . PHP_EOL, $iCurIndex, $size, ($size - $iCurIndex));

Note that PHP arrays are 0 based, so we need to add one to get the 1 of 10 kind of idea. If you don't need the actual value, you could use $i = 1; $i <= $size; ++$i) instead.

09-14-2009, 08:48 PM
Exactly! =)