473,770 Members | 2,096 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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_arr ay() 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.u k> wrote:
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?

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_arr ay() 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@$y ahoo$.co.uk> wrote:
On Fri, 31 Oct 2003 10:00:57 +0000, Jon Kraft <jo*@jonux.co.u k> wrote:
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.


<?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.u k> wrote:
Glutinous <glutinosity@$y ahoo$.co.uk> wrote:
On Fri, 31 Oct 2003 10:00:57 +0000, Jon Kraft <jo*@jonux.co.u k> wrote:
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.


<?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
3168
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: $myarray = $array(1, 2, 3, ... 100); echo 'Enter your changes, then click Submit:'; foreach ($array as $i) echo '<table tags> <input value="'.$i.'" name="index.'$i.'"> <table tags>';
1
2993
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 not work. code: -----
11
2549
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 like this:
5
1735
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 they want to despatch from a list of available ones. In many cases, these items will be of the same time, so the dropdown of available serial no's may be the same. So now I need to check that the user hasnt picked the same serial no for two...
6
4152
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 end is found is less efficient than using sizeof operator , since size of the array is completely determined at compile time. i don't quite understand this. Could anyone explain to me in detail ?
5
2764
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 words, in one foreach loop, if possible, I want to have the results as question0+answer0 and question1+answer1 in 2 separate strings entered into an array. Thanks much.
6
38519
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 through this without much trouble. Programming knowledge is not required. Index What is SQL? Why MySQL? Installing MySQL. Using the MySQL command line interface
5
5392
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 something like this? var $userArray = array(array());
5
2386
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 ADS_PROPERTY_APPEND, "wbempath, Array(Product & "|" and Key) Basically, a convenient place for me to store and manage CD installation keys.
0
9618
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10101
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8933
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.