Iterate from middle of array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ChronoFish

    Iterate from middle of array

    Hi there,

    I want to iterate through an array starting at a known index. However the indexes are not linear.

    For example I have an array of events keyed by timestamp.

    $eventList = array (1064263264 => "event1", 10642635555 => "event2", 1064266666 => "event3", 1064267782 => "event4", 1064268812 =>
    "event5");

    I basically want to do a "for" or "foreach" but I don't necessarily want to start at key 1064263264 (event1).

    I'm looking for something Like

    $startEvent = 10642635555;
    $endEvent = 1064267782;
    for ($event = $startEvent; $event < $lastEvent; $event = nextKey())
    {
    print $eventList[$event];
    }

    To make it worse, I can't use a foreach and simply "if" my way out of it because I am already iterating over the entire list and I
    would like to avoid a O(x^2) algorithm.

    foreach ($eventList as $currentEvent)
    {
    ..... // processing
    $startEvent = 10642635555;
    $endEvent = 1064267782;
    for ($event = $startEvent; $event < $lastEvent; $event = nextKey())
    {
    print $eventList[$event];
    }
    ..... // more processing
    }

    Of course there is no "nextKey()" and before rolling my own I thought I would ask if PHP already has a solution for this that I've
    missed.

    Thanks!
    CF



  • Pedro Graca

    #2
    Re: Iterate from middle of array

    ChronoFish wrote (in loooooooooooooo ooooooooooooooo oooong lines):[color=blue]
    > I want to iterate through an array starting at a known index.
    > However the indexes are not linear.[/color]
    [color=blue]
    > To make it worse, I can't use a foreach and simply "if" my way out
    > of it because I am already iterating over the entire list and I
    > would like to avoid a O(x^2) algorithm.
    >[/color]
    # foreach ($eventList as $currentEvent)
    # {


    // also use the index inside your loop
    foreach ($eventList as $k=>$currentEve nt)
    {

    [color=blue]
    > .... // processing
    > $startEvent = 10642635555;
    > $endEvent = 1064267782;[/color]

    // move the $startEvent and $endEvent outside the loop,
    // unless, of course, they are changing inside :)


    # for ($event = $startEvent; $event < $lastEvent; $event = nextKey())
    # {

    // use the $k thing
    if ($startEvent <= $k && $k <= $endEvent) print $currentEvent;


    # print $eventList[$event];
    # }[color=blue]
    > .... // more processing
    > }[/color]


    This way you only go through the array once.
    I really didn't understand if you wanted multiple prints of each event
    between $startEvent and $endEvent; if you do, this wil not work.
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--

    Comment

    • Justin Koivisto

      #3
      Re: Iterate from middle of array

      ChronoFish wrote:[color=blue]
      > I want to iterate through an array starting at a known index. However the indexes are not linear.
      >
      > For example I have an array of events keyed by timestamp.
      >
      > $eventList = array (1064263264 => "event1", 10642635555 => "event2", 1064266666 => "event3", 1064267782 => "event4", 1064268812 =>
      > "event5");
      >
      > I basically want to do a "for" or "foreach" but I don't necessarily want to start at key 1064263264 (event1).
      >
      > I'm looking for something Like
      >
      > $startEvent = 10642635555;
      > $endEvent = 1064267782;
      > for ($event = $startEvent; $event < $lastEvent; $event = nextKey())
      > {
      > print $eventList[$event];
      > }
      >
      > To make it worse, I can't use a foreach and simply "if" my way out of it because I am already iterating over the entire list and I
      > would like to avoid a O(x^2) algorithm.
      >
      > foreach ($eventList as $currentEvent)
      > {
      > .... // processing
      > $startEvent = 10642635555;
      > $endEvent = 1064267782;
      > for ($event = $startEvent; $event < $lastEvent; $event = nextKey())
      > {
      > print $eventList[$event];
      > }
      > .... // more processing
      > }
      >
      > Of course there is no "nextKey()" and before rolling my own I thought I would ask if PHP already has a solution for this that I've
      > missed.[/color]

      Here's my first thought...

      $tmp=$eventList ; // ksort doesn't make a copy of the passed array,
      ksort($tmp); // it modifies it - need to copy if you want the
      // original for use later

      foreach($tmp as $k=>$event){
      if($k<$startEve nt) continue; // not at the start yet
      else if($k>$endEvent ) break; // last one has been done
      else echo $event; // print it out
      }
      unset($tmp);

      --
      Justin Koivisto - spam@koivi.com
      PHP POSTERS: Please use comp.lang.php for PHP related questions,
      alt.php* groups are not recommended.
      SEO Competition League: http://seo.koivi.com/

      Comment

      • ChronoFish

        #4
        Re: Iterate from middle of array


        "Pedro Graca" <hexkid@hotpop. com> wrote in message news:c22dpo$1p4 bac$1@ID-
        .....[color=blue]
        > This way you only go through the array once.
        > I really didn't understand if you wanted multiple prints of each event
        > between $startEvent and $endEvent; if you do, this wil not work.[/color]


        Thanks so much Pedro. I don't think I stated my intentions strong enough. I do need to go through multiple times as startEvent and
        endEvent change on each iteration. I endedup just biteing the bullet and iterate twice through.

        I think what I was after was the opposite of the pos() function. It gives you the current key. What I want to do is to be able to
        set the "current" position of the array position pointer.

        Thanks anyway!

        CF


        Comment

        • ChronoFish

          #5
          Re: Iterate from middle of array


          "Justin Koivisto" <spam@koivi.com > wrote in message news:%H31c.431$ 6c5.16535@news7 .onvoy.net...
          [color=blue]
          > $tmp=$eventList ; // ksort doesn't make a copy of the passed array,
          > ksort($tmp); // it modifies it - need to copy if you want the
          > // original for use later
          >
          > foreach($tmp as $k=>$event){
          > if($k<$startEve nt) continue; // not at the start yet
          > else if($k>$endEvent ) break; // last one has been done
          > else echo $event; // print it out
          > }
          > unset($tmp);
          >[/color]

          Hi Justin, Thanks for your help. This is what I was trying to avoid as this gives me 0(n^2) (for every element I iterate through
          ever element). As noted in my other response what I was looking for was the opposite of the pos() function. pos() returns the
          current key of the arrays internal position pointer. I want to be able to to set it like pos($currentEve nt) and continue iterating
          from there. Oh well....

          Thanks again,
          CF


          Comment

          • Chung Leong

            #6
            Re: Iterate from middle of array

            Try this:

            $indices = array_keys($eve ntList);
            $start = array_search($i ndices, $startEvent);
            $end = array_search($i ndices, $endEvent);
            $range = array_slice($ev entList, $start, $end - $start + 1);

            or this

            $positions = array_flip(arra y_keys($eventLi st));
            $start = $positions[$startEvent];
            $end = $positions[$endEvent];
            $range = array_slice($ev entList, $start, $end - $start + 1);

            $range will be the elements you're interested in.

            Uzytkownik "ChronoFish " <deja@chronofis h.com> napisal w wiadomosci
            news:bs21c.8082 $oP.2717@lakere ad03...[color=blue]
            > Hi there,
            >
            > I want to iterate through an array starting at a known index. However the[/color]
            indexes are not linear.[color=blue]
            >
            > For example I have an array of events keyed by timestamp.
            >
            > $eventList = array (1064263264 => "event1", 10642635555 => "event2",[/color]
            1064266666 => "event3", 1064267782 => "event4", 1064268812 =>[color=blue]
            > "event5");
            >
            > I basically want to do a "for" or "foreach" but I don't necessarily want[/color]
            to start at key 1064263264 (event1).[color=blue]
            >
            > I'm looking for something Like
            >
            > $startEvent = 10642635555;
            > $endEvent = 1064267782;
            > for ($event = $startEvent; $event < $lastEvent; $event = nextKey())
            > {
            > print $eventList[$event];
            > }
            >
            > To make it worse, I can't use a foreach and simply "if" my way out of it[/color]
            because I am already iterating over the entire list and I[color=blue]
            > would like to avoid a O(x^2) algorithm.
            >
            > foreach ($eventList as $currentEvent)
            > {
            > .... // processing
            > $startEvent = 10642635555;
            > $endEvent = 1064267782;
            > for ($event = $startEvent; $event < $lastEvent; $event = nextKey())
            > {
            > print $eventList[$event];
            > }
            > .... // more processing
            > }
            >
            > Of course there is no "nextKey()" and before rolling my own I thought I[/color]
            would ask if PHP already has a solution for this that I've[color=blue]
            > missed.
            >
            > Thanks!
            > CF
            >
            >
            >[/color]


            Comment

            • ChronoFish

              #7
              Re: Iterate from middle of array

              "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message news:<DPCdnZnuZ dopE9vd4p2dnA@c omcast.com>...[color=blue]
              > Try this:
              >
              > $indices = array_keys($eve ntList);
              > $start = array_search($i ndices, $startEvent);
              > $end = array_search($i ndices, $endEvent);
              > $range = array_slice($ev entList, $start, $end - $start + 1);
              >[/color]

              Great idea. I had not considered slice and I think this will work
              fine. Not sure what it means in terms of "Big O" but it looks clean.

              Thanks!
              CF

              Comment

              Working...