Retrieving previous array item

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

    Retrieving previous array item

    I have an array. The array key is the id of the item (for example
    categories[91], 91 is the ID of the category) all ID are not exactly in
    date order, but they're added to the array in date order, how do I find
    the previous (in the sense of when it was added) array entry of, for
    example category['91']?

    --

    Belmin Fernandez

    Visit: http://www.belminfernandez.com/homepage
    Email: belminf at gmail period com
  • JDS

    #2
    Re: Retrieving previous array item

    On Wed, 23 Feb 2005 17:48:05 +0000, Belmin wrote:
    [color=blue]
    > I have an array. The array key is the id of the item (for example
    > categories[91], 91 is the ID of the category) all ID are not exactly in
    > date order, but they're added to the array in date order, how do I find
    > the previous (in the sense of when it was added) array entry of, for
    > example category['91']?[/color]

    How are items added to the array? I don't understand how, if "all ID are
    not exactly in date order," the items are added in date order.

    In any case, any solutions based on your current array will be unreliable.

    To make your data reliable, you will need to add the date to the data
    somehow. I suggest a 2D associative array along the lines of

    $categories = array(
    array( "id" => 91, "date_added " => "2005-02-23 10:30:33" )
    , array( "id" => 92, "date_added " => "2005-02-23 12:19:52")
    );

    (the time stamp can be formatted however you wish)

    Then you can actually do stuff with the real date value and be certain
    that it is correct in comparison to other items in the array.

    --
    JDS | jeffrey@example .invalid
    | http://www.newtnotes.com
    DJMBS | http://newtnotes.com/doctor-jeff-master-brainsurgeon/

    Comment

    • Geoff Berrow

      #3
      Re: Retrieving previous array item

      I noticed that Message-ID:
      <pan.2005.02.23 .18.30.18.58453 9@example.inval id> from JDS contained the
      following:
      [color=blue]
      >How are items added to the array? I don't understand how, if "all ID are
      >not exactly in date order," the items are added in date order.
      >[/color]

      Yeah, I wondered about that too...

      --
      Geoff Berrow (put thecat out to email)
      It's only Usenet, no one dies.
      My opinions, not the committee's, mine.
      Simple RFDs http://www.ckdog.co.uk/rfdmaker/

      Comment

      • Belmin

        #4
        Re: Retrieving previous array item

        What I meant to say, the ID's aren't in date order. So for example, ID
        91 might actually be older than ID 10. And also, not all IDs are used so
        an array could look like:

        array[10]
        array[91]
        array[23]
        array[49]
        (and so on)

        Though during creation, they're stacked in date order. Im not sure if
        I'm being clear enough. What I ended up doing is doing an array_keys
        function and going by the index of it's results. Is this the most
        efficient way?

        Written by "JDS" on 2/23/05 1:30p:[color=blue]
        > On Wed, 23 Feb 2005 17:48:05 +0000, Belmin wrote:
        >
        >[color=green]
        >> I have an array. The array key is the id of the item (for example
        >>categories[91], 91 is the ID of the category) all ID are not exactly in
        >>date order, but they're added to the array in date order, how do I find
        >>the previous (in the sense of when it was added) array entry of, for
        >>example category['91']?[/color]
        >
        >
        > How are items added to the array? I don't understand how, if "all ID are
        > not exactly in date order," the items are added in date order.
        >
        > In any case, any solutions based on your current array will be unreliable.
        >
        > To make your data reliable, you will need to add the date to the data
        > somehow. I suggest a 2D associative array along the lines of
        >
        > $categories = array(
        > array( "id" => 91, "date_added " => "2005-02-23 10:30:33" )
        > , array( "id" => 92, "date_added " => "2005-02-23 12:19:52")
        > );
        >
        > (the time stamp can be formatted however you wish)
        >
        > Then you can actually do stuff with the real date value and be certain
        > that it is correct in comparison to other items in the array.
        >[/color]

        --

        Belmin Fernandez

        Visit: http://www.belminfernandez.com/homepage
        Email: belminf at gmail period com

        Comment

        • Wayne

          #5
          Re: Retrieving previous array item

          On Wed, 23 Feb 2005 19:28:14 GMT, Belmin <anonymous@nosp am.com> wrote:
          [color=blue]
          >What I meant to say, the ID's aren't in date order. So for example, ID
          >91 might actually be older than ID 10. And also, not all IDs are used so
          >an array could look like:
          >
          >array[10]
          >array[91]
          >array[23]
          >array[49]
          >(and so on)
          >
          >Though during creation, they're stacked in date order. Im not sure if
          >I'm being clear enough. What I ended up doing is doing an array_keys
          >function and going by the index of it's results. Is this the most
          >efficient way?[/color]

          Try the end() function.

          Comment

          • Chung Leong

            #6
            Re: Retrieving previous array item

            "Belmin" <anonymous@nosp am.com> wrote in message
            news:iP4Td.1328 4$qn2.2736440@t wister.nyc.rr.c om...[color=blue]
            > Though during creation, they're stacked in date order. Im not sure if
            > I'm being clear enough. What I ended up doing is doing an array_keys
            > function and going by the index of it's results. Is this the most
            > efficient way?[/color]

            Nah, there isn't a efficient way to retrieve the previous item or index.
            array_keys() followed by array_flip() is probably the most straight forward
            way.


            Comment

            • Michael Fesser

              #7
              Re: Retrieving previous array item

              .oO(Chung Leong)
              [color=blue]
              >"Belmin" <anonymous@nosp am.com> wrote in message
              >news:iP4Td.132 84$qn2.2736440@ twister.nyc.rr. com...[color=green]
              >> Though during creation, they're stacked in date order. Im not sure if
              >> I'm being clear enough. What I ended up doing is doing an array_keys
              >> function and going by the index of it's results. Is this the most
              >> efficient way?[/color]
              >
              >Nah, there isn't a efficient way to retrieve the previous item or index.[/color]

              prev() exists. Only problem is that you can't explicitly set the
              internal array pointer to a particular element.

              Micha

              Comment

              Working...