SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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 Code:
    <?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 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..

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You can do exactly as you did in the first loop:
    PHP Code:
    for ($i 0$i count($ages); $i++) {
      echo 
    $ages[$i];
      if (
    $i count($ages) - 1) {
        break;
      }
      echo 
    ', ';

    But there's an easier way

    PHP Code:
    echo implode(', '$ages); 

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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..

  4. #4
    SitePoint Addict
    Join Date
    Mar 2005
    Location
    Toronto
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Theoretically yes, but you need a counter still...it's basically same idea as for loop

    PHP Code:
    $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...
    In search of the clouds...and maybe some work

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Kefeso..

  6. #6
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

    PHP Code:
    if (next($ages) === false) ... 

  7. #7
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You can try this for loop.. for learning purposes:

    PHP Code:
    $ages = array(2358121418);
    $count count($ages);

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


  8. #8
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    That will end the loop prematurely if the last element in the array appears anywhere earlier in the array as well.

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    let's make this simpler by displaying the first term before the loop

    Code php:
    $ages = array(2, 3, 5, 8, 12, 14, 18); 
     
    echo current($ages);
    while ($age = next($ages)) {
          echo ', ' . $age;
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  10. #10
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    reset() is necessary for such ancient techniques

  11. #11
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Shrapnel_N5 View Post
    reset() is necessary for such ancient techniques
    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.

    Code php:
    $array = array('step one', 'step two', 'step three', 'step four');
     
    // by default, the pointer is on the first element
    echo current($array) . "<br />\n"; // "step one"
     
    // skip two steps
    next($array);
    next($array);
    echo current($array) . "<br />\n"; // "step three"
     
    // reset pointer, start again on step one
    reset($array);
    echo current($array) . "<br />\n"; // "step one"
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  12. #12
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  13. #13
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  14. #14
    SitePoint Enthusiast
    Join Date
    Jan 2008
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

    PHP Code:
    $ages = array(311232436543);
    for (
    $i 0$i count($ages); $i++) {
        
    $value = ($i != count($ages) - 1) ? ',' '';
        echo 
    $ages[$i] . $value;


  15. #15
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by staar2 View Post
    As it's previously told this is not the best case where to use while loop
    Please explain?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  16. #16
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's only when
    Here is the problem. Countless bugs made from such assumptions.
    "I won't check/initialize because it's just unnecessary"
    But I won't press on it. It comes only with some experience. Negative experience

  17. #17
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    I don't blame you for being twitchy, so please explain how the following code can result in anything else but the expected result?

    Code php:
    $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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  18. #18
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    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:

    Code php:
    $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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

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