SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Opinions on using each() to help with iteratation

    Just wondering if anyone has any better ideas for how to do this;

    PHP Code:
    <?php
    class SomeData {
        var 
    $data;

        function 
    SomeData () {
            
    $this->data=range(1,10);
        }

        function 
    get() {
            
    $data=each($this->data);
            return 
    $data['value'];
        }

        function 
    reset () {
            
    reset($this->data);
        }
    }

    $sd=new SomeData;

    while ( 
    $data $sd->get() ) {
        echo ( 
    $data.'<br />' );
    }
    ?>
    The point is to have a simple way to iterate through the contents of a object property in a manner which "feels" much like get rows from a database in terms of how you use it.

    Search as I might I can find a better alternative to each() for a PHP function which gets the current value of an array then moves the point forward by one.

    The only problem is this means have to assign the result to another variable before returning it (and I'm searching for perfection);

    PHP Code:
        function get() {
            
    $data=each($this->data);
            return 
    $data['value'];
        } 
    Of course this won't work

    PHP Code:
        function get() {
            return 
    current($this->data);
            
    next ($this->data)
        } 
    As next() will never be called.

  2. #2
    SitePoint Zealot
    Join Date
    Mar 2002
    Location
    Perth, Australia
    Posts
    164
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HarryF, I'm not sure if I fully understand you, because I can't see why you don't just use foreach?

    PHP Code:
    class SomeData {
        var 
    $data;

        function 
    SomeData () {
            
    $this->data=range(1,10);
        }
    }

    $sd=new SomeData;

    foreach(
    $sd->data as $item){
        echo 
    $item "<br />";


  3. #3
    SitePoint Member
    Join Date
    Jan 2003
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Harry you can just use next() to return a copy of the value in $data and will automatically advance to the next value in the array.

  4. #4
    SitePoint Member
    Join Date
    Jan 2003
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry
    Please disregard my last post I was backwards on the order for next(). I just realised my mistake.

  5. #5
    Super Ninja Monkey Travis's Avatar
    Join Date
    Dec 2001
    Location
    Sioux City, Iowa
    Posts
    691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not:
    PHP Code:
     function get() {
            
    $temp current($this->data);
            
    next ($this->data);
            return 
    $temp;
      } 
    ??
    Travis Watkins - Hyperactive Coder
    My Blog: Realist Anew
    Projects: Alacarte - Gnome Menu Editor

  6. #6
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    because I can't see why you don't just use foreach?
    You're right - I could access the properties directly but I prefer using a method because it makes the API to the class clear (I don't really want people access the properties directly).

    **function get() {
    ********$temp = current($this->data);
    ********next ($this->data);
    ********return $temp;
    **}
    I guess that's an alternative but that's increased it to three lines of code

    The function which is almost perfect is array_shift() but the has the side effect of deleting the elements it returns which I dont want

  7. #7
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by HarryF
    I guess that's an alternative but that's increased it to three lines of code [img]images/smilies/wink.gif[/img]
    PHP Code:
       function get() { 
            
    $temp current($this->data); next ($this->data); return $temp
      } 
    Work smarter, not harder. -Scrooge McDuck

  8. #8
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm - I see

  9. #9
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    LoL......
    .
    .
    .
    LoL...... Robo's taking the piss I think ? LoL.....

  10. #10
    SitePoint Addict been's Avatar
    Join Date
    May 2002
    Location
    Gent, Belgium
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you want to get rid of the temp variable, I guess you should implement two (integer) counters: one holding the total amount of elements in the array, the other the current index of the array. Then in the get() method:
    PHP Code:
    $this->current += 1;
    return (
    $this->current $this->count )
        ? 
    $this->data[$this->current 1]
        : 
    false
    .... ?
    Of course, you'll need more than 2 lines of code, but hey, at least you got rid of the temp var ;-).

    Or you could have a method returning an Eclipse iterator, but I guess you already explored that option ;-)

    Just some (stupid?) ideas ...
    Per
    Everything
    works on a PowerPoint slide

  11. #11
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can't really see what the problem is really - I can't see how the solution can be done using only one line of code.

    I think this is taking optimisation way over the top, the solution by Trav is more than optimised IMHO.


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
  •