473,396 Members | 1,914 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,396 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 2327
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.