473,323 Members | 1,547 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,323 software developers and data experts.

accessing numerical array index in foreach

Hello everyone,

I have no trouble looping through an array using foreach. To keep
track of the index, I set a variable before the loop and increment it
in the loop:

$index = 0;
foreach ($array as $item) {
blah;
$index++;
do something that requires knowing the index of the current item;
}

Is there a better way to access that index value?

Thanks.

Mar 21 '07 #1
4 17536
e_*******@hotmail.com wrote:
I have no trouble looping through an array using foreach. To keep
track of the index, I set a variable before the loop and increment it
in the loop:
foreach ($array as $index =$item) {
blah;
do something that requires knowing the index of the current item;
}

I didn't notice someone shut www.php.net down along wih all the necessary
manuals...

http://www.php.net/manual/en/control...es.foreach.php

--
Pawel Kraszewski
www.kraszewscy.net

Mar 21 '07 #2
e_*******@hotmail.com wrote:
Hello everyone,

I have no trouble looping through an array using foreach. To keep
track of the index, I set a variable before the loop and increment it
in the loop:

$index = 0;
foreach ($array as $item) {
blah;
$index++;
do something that requires knowing the index of the current item;
}

Is there a better way to access that index value?
You might get a better result with this:

foreach ($array as $index=>$item) {
// blah;
// do something else;
}

--
Kim André Akerø
- ki******@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)
Mar 21 '07 #3
e_*******@hotmail.com schrieb:
Hello everyone,

I have no trouble looping through an array using foreach. To keep
track of the index, I set a variable before the loop and increment it
in the loop:

$index = 0;
foreach ($array as $item) {
blah;
$index++;
do something that requires knowing the index of the current item;
}

Is there a better way to access that index value?

Thanks.
Yes, there is a better way.

foreach ($array as $key =$item)
{
blah;
}

You should read through php.net/foreach ;-)
Mar 22 '07 #4
I have no trouble looping through an array using foreach. To keep
track of the index, I set a variable before the loop and increment it
in the loop:

$index = 0;
foreach ($array as $item) {
blah;
$index++;
do something that requires knowing the index of the current item;
}

Is there a better way to access that index value?
As mike and kim suggested use foreach ($array as $index=>$item) {

Also something that has not been raised. You pseudo exemple is somewhat
flawed so I thought it best to point it out.

In your example you are incrementing the index before you use it and the
index starts at 0. Array's are zero based meaning the first element of the
array has the key 0 the second 1 etc. In your example you would have 1 as
being the first and 2 being the second.:-

<?php
$test = array('a','b');
print_r($test)
?>

The above code shows this.
Mar 24 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: 2mc | last post by:
I'm new to Python and to Numerical Python. I have a program written in another program that used arrays extensively. I'm trying to convert to Python. Assume an array in Numerical Python, the...
6
by: Chris Styles | last post by:
Dear All, I've been using some code to verify form data quite happily, but i've recently changed the way my form is structured, and I can't get it to work now. Originally : The form is...
32
by: Joe Rattz | last post by:
Hmmm, I wrote the following code. I want an array of bools and I want to intialize them to false. bool bits = new bool; foreach(bool bit in bits) { bit = false; } The compiler complains...
4
by: Arjen | last post by:
Hi, I need to add this inside an array: 1 3 2 4 3 2 4 5 5 1 I think of using this:
16
by: Martin Jørgensen | last post by:
Hi, I've made a program from numerical recipes. Looks like I'm not allowed to distribute the source code from numerical recipes but it shouldn't even be necessary to do that. My problem is...
4
by: aqazi | last post by:
Hi guys I am having a problem with arrayu manipulation. in my php script i am reading from a csv file. the content of file is like this: name,color,quantity,price; apple,red,10,$2;...
7
by: Andrew C | last post by:
Hi, folks. I've encountered what seems to me to be something of an oddity while playing around with XML parsing in PHP, and I wondered if any of you might be able to clear up my confusion... ...
2
by: z1 | last post by:
hi - i downloaded some data from a weather service that is in an xml file. =object(SimpleXMLElement)#9 (4) { =array(2) { =object(SimpleXMLElement)#11 (2) { =string(25) "Daily Maximum Temperature"...
4
by: BravoFoxtrot | last post by:
Hi, I'm trying to build an index into a multi dimensional associative array. I may not know how many dimensions there are so i want to pass the array indexes as a variable. $arrayToAccess =...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.