By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,729 Members | 1,339 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,729 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
Jul 17 '05 #2

P: n/a
On Fri, 31 Oct 2003 10:00:57 +0000, Jon Kraft <jo*@jonux.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.

Jul 17 '05 #3

P: n/a
Glutinous <glutinosity@$yahoo$.co.uk> wrote:
On Fri, 31 Oct 2003 10:00:57 +0000, Jon Kraft <jo*@jonux.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>

http://uk.php.net/manual/en/function.each.php

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

Jul 17 '05 #4

P: n/a
On Mon, 03 Nov 2003 10:46:21 +0000, Jon Kraft <jo*@jonux.co.uk> wrote:
Glutinous <glutinosity@$yahoo$.co.uk> wrote:
On Fri, 31 Oct 2003 10:00:57 +0000, Jon Kraft <jo*@jonux.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>

http://uk.php.net/manual/en/function.each.php


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!

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.