473,231 Members | 1,984 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

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.

Jul 17 '05 #1
4 2320
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Raptor | last post by:
I'm using a single script to generate a table with <input>s in each row. I fill the array with initial values, then write it out to the table and let the user edit the values. Something like: ...
1
by: H. Adam | last post by:
hi ! i m trying to calculate an average array from an 2d data array. my problem is that my function does not generate an average array but just one value. but i do not understand why it does...
11
by: Stefan Richter | last post by:
Hi, I want to create an associative Array with a PHP variable (article ID) as Key and another associative array as it's value. How do I instanciate it, how can I fill it? I want something...
5
by: CJM | last post by:
In an application I'm working the user has the opportunity to record the despatching of one or more items with serial numbers. For each item they despatch, they have to chose the serial no that...
6
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the...
5
by: J Huntley Palmer | last post by:
How may I concatenate the index respective questions and answers into one string per index using ONE foreach loop? I do not want to use numerical indexes just the named ones as below. In other...
6
Atli
by: Atli | last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users. Anyone should be able to get...
5
by: KDawg44 | last post by:
Hi, Is there a way to get a multidimensional associative array with the entire result set? I would like to get a an array like this: resultsArray How can I accomplish this? Can I do...
5
by: Mark B | last post by:
Hi experts, I'm converting a homebrew AD management progam I wrote, from VB6 to VB 2008. I've got some code that sticks values in to Active Directory like this:- objOU.PutEx...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.