Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2009
    Posts
    62
    Thanks
    19
    Thanked 1 Time in 1 Post

    Array remaining size from an index

    Heya!
    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..

    Thanks!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    PHP Code:
    $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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    SystemJay (09-14-2009)

  • #3
    New Coder
    Join Date
    Mar 2009
    Posts
    62
    Thanks
    19
    Thanked 1 Time in 1 Post
    Exactly! =)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •