Professional Web Applications Themes

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

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....

  1. #1

    Default 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.

    Glutinous Guest

  2. #2

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

    Glutinous <glutinosity$yahoo$.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?
    >
    > 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!
    Where do you get your array from?

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

    JOn
    Jon Kraft Guest

  3. #3

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

    On Fri, 31 Oct 2003 10:00:57 +0000, Jon Kraft <jonjonux.co.uk> wrote:
    >Glutinous <glutinosity$yahoo$.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?
    >>
    >> 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!
    >
    >Where do you get your array from?
    >
    >Do you use mysql_fetch_array() by any chance? By default it returns both an
    >indexed and an associative array.
    >
    >JOn
    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.

    Glutinous Guest

  4. #4

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

    Glutinous <glutinosity$yahoo$.co.uk> wrote:
    > On Fri, 31 Oct 2003 10:00:57 +0000, Jon Kraft <jonjonux.co.uk> wrote:
    >
    >>Glutinous <glutinosity$yahoo$.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_array() by any chance? By default it returns both
    >>an indexed and an associative array.
    >
    > <?php
    > $foo = array("FirstKey" => "1stValue");
    > $bar = each($foo);
    > print_r($bar);
    > ?>
    >
    > Which produces:
    > Array ( [1] => 1stValue [value] => 1stValue [0] => FirstKey [key] =>
    > FirstKey )
    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>

    [url]http://uk.php.net/manual/en/function.each.php[/url]

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

    Jon Kraft Guest

  5. #5

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

    On Mon, 03 Nov 2003 10:46:21 +0000, Jon Kraft <jonjonux.co.uk> wrote:
    >Glutinous <glutinosity$yahoo$.co.uk> wrote:
    >
    >> On Fri, 31 Oct 2003 10:00:57 +0000, Jon Kraft <jonjonux.co.uk> wrote:
    >>
    >>>Glutinous <glutinosity$yahoo$.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_array() by any chance? By default it returns both
    >>>an indexed and an associative array.
    >>
    >> <?php
    >> $foo = array("FirstKey" => "1stValue");
    >> $bar = each($foo);
    >> print_r($bar);
    >> ?>
    >>
    >> Which produces:
    >> Array ( [1] => 1stValue [value] => 1stValue [0] => FirstKey [key] =>
    >> FirstKey )
    >
    >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>
    >
    >[url]http://uk.php.net/manual/en/function.each.php[/url]
    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!

    Glutinous Guest

Similar Threads

  1. ActionScript Array Duplication
    By kdennis4 in forum Macromedia Flex General Discussion
    Replies: 0
    Last Post: April 25th, 12:40 PM
  2. Folder and file duplication
    By DaveGreen01 in forum Macromedia Contribute General Discussion
    Replies: 1
    Last Post: July 11th, 08:21 PM
  3. Connection duplication
    By Skags1 in forum Macromedia Contribute Connection Administrtion
    Replies: 0
    Last Post: July 3rd, 11:00 PM
  4. Symbol Duplication MX Style
    By RabRay webforumsuser@macromedia.com in forum Macromedia Fireworks
    Replies: 2
    Last Post: July 4th, 07:32 AM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139