Basic array stuff... Why [0] & ['key'] and [1] & ['value'] 'duplication'?

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

    #1

    Basic array stuff... Why [0] & ['key'] and [1] & ['value'] 'duplication'?

    I've been studying this for hours, searching the www & usenet, and
    still can't figure out why 'each' returns an array of four key/value
    pairs, when it looks like just two pairs would suffice...

    That is, it looks to me like
    [0] & ['key']
    are the same, and also
    [1] & ['value'],
    so what's the point of duplicating them?

    I'm assuming that in fact there are serious differences between
    [0] and ['key']
    and between
    [1] and [value],
    but I haven't a clue what that might be.

    Any hints, URLs, explanaitons, etc. most welcome!

    Thank you.

  • Jon Kraft

    #2
    Re: Basic array stuff... Why [0] & ['key'] and [1] & ['value'] 'duplication'?

    Glutinous <glutinosity@$y ahoo$.co.uk> wrote:
    [color=blue]
    > I've been studying this for hours, searching the www & usenet, and
    > still can't figure out why 'each' returns an array of four key/value
    > pairs, when it looks like just two pairs would suffice...
    >
    > That is, it looks to me like
    > [0] & ['key']
    > are the same, and also
    > [1] & ['value'],
    > so what's the point of duplicating them?
    >
    > I'm assuming that in fact there are serious differences between
    > [0] and ['key']
    > and between
    > [1] and [value],
    > but I haven't a clue what that might be.
    >
    > Any hints, URLs, explanaitons, etc. most welcome![/color]

    Where do you get your array from?

    Do you use mysql_fetch_arr ay() by any chance? By default it returns both an
    indexed and an associative array.

    JOn

    Comment

    • Glutinous

      #3
      Re: Basic array stuff... Why [0] &amp; ['key'] and [1] &amp; ['value'] 'duplication'?

      On Fri, 31 Oct 2003 10:00:57 +0000, Jon Kraft <jon@jonux.co.u k> wrote:
      [color=blue]
      >Glutinous <glutinosity@$y ahoo$.co.uk> wrote:
      >[color=green]
      >> I've been studying this for hours, searching the www & usenet, and
      >> still can't figure out why 'each' returns an array of four key/value
      >> pairs, when it looks like just two pairs would suffice...
      >>
      >> That is, it looks to me like
      >> [0] & ['key']
      >> are the same, and also
      >> [1] & ['value'],
      >> so what's the point of duplicating them?
      >>
      >> I'm assuming that in fact there are serious differences between
      >> [0] and ['key']
      >> and between
      >> [1] and [value],
      >> but I haven't a clue what that might be.
      >>
      >> Any hints, URLs, explanaitons, etc. most welcome![/color]
      >
      >Where do you get your array from?
      >
      >Do you use mysql_fetch_arr ay() by any chance? By default it returns both an
      >indexed and an associative array.
      >
      >JOn[/color]

      Thanks for the response, Jon.

      Actually, I'm wading my way through tutorial books, and this aspect
      came up (presumably on its way to discussing mysql).

      One piece of php code I've been using to try to understand this aspect
      is:

      <?php
      $foo = array("FirstKey " => "1stValue") ;
      $bar = each($foo);
      print_r($bar);
      ?>

      Which produces:
      Array ( [1] => 1stValue [value] => 1stValue [0] => FirstKey [key] =>
      FirstKey )

      and I just don't see the point of having both [0] & ['key'], when they
      appear to represent the same thing, and similarly [1] & ['value'],

      I dare say I can live with it, but I don't like skipping over
      something too lightly, and I'd welcome any further attempt to
      enlighten me!

      Thanks again.

      Comment

      • Jon Kraft

        #4
        Re: Basic array stuff... Why [0] &amp; ['key'] and [1] &amp; ['value'] 'duplication'?

        Glutinous <glutinosity@$y ahoo$.co.uk> wrote:
        [color=blue]
        > On Fri, 31 Oct 2003 10:00:57 +0000, Jon Kraft <jon@jonux.co.u k> wrote:
        >[color=green]
        >>Glutinous <glutinosity@$y ahoo$.co.uk> wrote:
        >>[color=darkred]
        >>> I've been studying this for hours, searching the www & usenet, and
        >>> still can't figure out why 'each' returns an array of four key/value
        >>> pairs, when it looks like just two pairs would suffice...
        >>>
        >>> That is, it looks to me like
        >>> [0] & ['key']
        >>> are the same, and also
        >>> [1] & ['value'],
        >>> so what's the point of duplicating them?[/color]
        >>
        >>Where do you get your array from?
        >>
        >>Do you use mysql_fetch_arr ay() by any chance? By default it returns both
        >>an indexed and an associative array.[/color]
        >
        > <?php
        > $foo = array("FirstKey " => "1stValue") ;
        > $bar = each($foo);
        > print_r($bar);
        > ?>
        >
        > Which produces:
        > Array ( [1] => 1stValue [value] => 1stValue [0] => FirstKey [key] =>
        > FirstKey )[/color]

        Hi,

        That's just default behaviour of each().

        <quote>
        Returns the current key and value pair from the array array and advances the
        array cursor. This pair is returned in a four-element array, with the keys
        0, 1, key, and value. Elements 0 and key contain the key name of the array
        element, and 1 and value contain the data.
        </quote>

        Return the current key and value pair from an array and advance the array cursor


        --
        To a Californian, the basic difference between the people and the pigeons
        in New York is that the pigeons don't shit on each other.
        -- From "East vs. West: The War Between the Coasts

        Comment

        • Glutinous

          #5
          Re: Basic array stuff... Why [0] &amp; ['key'] and [1] &amp; ['value'] 'duplication'?

          On Mon, 03 Nov 2003 10:46:21 +0000, Jon Kraft <jon@jonux.co.u k> wrote:
          [color=blue]
          >Glutinous <glutinosity@$y ahoo$.co.uk> wrote:
          >[color=green]
          >> On Fri, 31 Oct 2003 10:00:57 +0000, Jon Kraft <jon@jonux.co.u k> wrote:
          >>[color=darkred]
          >>>Glutinous <glutinosity@$y ahoo$.co.uk> wrote:
          >>>
          >>>> I've been studying this for hours, searching the www & usenet, and
          >>>> still can't figure out why 'each' returns an array of four key/value
          >>>> pairs, when it looks like just two pairs would suffice...
          >>>>
          >>>> That is, it looks to me like
          >>>> [0] & ['key']
          >>>> are the same, and also
          >>>> [1] & ['value'],
          >>>> so what's the point of duplicating them?
          >>>
          >>>Where do you get your array from?
          >>>
          >>>Do you use mysql_fetch_arr ay() by any chance? By default it returns both
          >>>an indexed and an associative array.[/color]
          >>
          >> <?php
          >> $foo = array("FirstKey " => "1stValue") ;
          >> $bar = each($foo);
          >> print_r($bar);
          >> ?>
          >>
          >> Which produces:
          >> Array ( [1] => 1stValue [value] => 1stValue [0] => FirstKey [key] =>
          >> FirstKey )[/color]
          >
          >Hi,
          >
          >That's just default behaviour of each().
          >
          ><quote>
          >Returns the current key and value pair from the array array and advances the
          >array cursor. This pair is returned in a four-element array, with the keys
          >0, 1, key, and value. Elements 0 and key contain the key name of the array
          >element, and 1 and value contain the data.
          ></quote>
          >
          >http://uk.php.net/manual/en/function.each.php[/color]

          Thank you again, Jon...

          So we have two pairs of data; and in each pair, one component is
          effectively redundant.

          That is, there is no use for [0] and [1], as 'key' and 'value' provide
          all the info that's available or useful?

          Weird kinda default behaviour to set up, though, isn't it? I could
          understand it if, say, [0] always held a numerical key, so no matter
          what you called it, it was always possible to refer to [0] to get a
          key/value pair's position, or something.

          Oh well...

          Thanks again for the responses!

          Comment

          Working...