tags:

views:

8098

answers:

7

Hi all,

Is it possible to find the foreach index?

in a "for" loop as follows: for($i = 0; $i < 10; ++$i){ echo $i.' '; }

$i will give you the index.

Do I have to use the for loop or is there some way to get the index in the foreach loop?

+24  A: 
foreach($array as $key=>$value) {
//  do stuff
}

$key is the index of each $array element

Owen
Depends on what the OP means by index: <? $a = array(3,5,'xx',4312,'sasas'); unset($a[3]); foreach ($a as $k=>$v) print "\$k= $k and \$v = $v"; ?>
Milen A. Radev
definitely, this question isn't very specific, i took it to mean the OP was largely unaware of the $key=>$value syntax
Owen
A: 

You can create $i outside the loop and do $i++ at the bottom of the loop.

Ólafur Waage
It's important to note that this approach gives the current iteration of the loop, NOT the current index of the iterated array.
Peter Bailey
+1  A: 

You can put a hack in your foreach, such as a field incremented on each run-through, which is exactly what the for loop gives you in a numerically-indexed array. Such a field would be a pseudo-index that needs manual management (increments, etc).

A foreach will give you your index in the form of your $key value, so such a hack shouldn't be necessary.

e.g., in a foreach

$index = 0;
foreach($data as $key=>$val)
{
    // Use $key as an index, or...

    // ... manage the index this way..
    echo "Index is $index\n";
    $index++;
}
ConroyP
+2  A: 

Please read my answer here (note, C# but the concept is the same in PHP):

http://stackoverflow.com/questions/43021/c-get-index-of-current-foreach-iteration#43029

Cliffnotes:

Foreach works with an iterator, so the index is never known.

FlySwat
+1  A: 

Jonathan is correct. PHP arrays act as a map table mapping keys to values. in some cases you can get an index if your array is defined, such as

$var = array(2,5);

for ($i = 0; $i < count($var); $i++) {
    echo $var[$i]."\n";
}

your output will be

2
5

in which case each element in the array has a knowable index, but if you then do something like the following

$var = array_push($var,10);

for ($i = 0; $i < count($var); $i++) {
    echo $var[$i]."\n";
}

you get no output. This happens because arrays in PHP are not linear structures like they are in most languages. They are more like hash tables that may or may not have keys for all stored values. Hence foreach doesn't use indexes to crawl over them because they only have an index if the array is defined. If you need to have an index, make sure your arrays are fully defined before crawling over them, and use a for loop.

The Brawny Man
good information
Owen
+2  A: 

Owen has a good answer. If you want just the key, and you are working with an array this might also be useful.

foreach(array_keys($array) as $key) {
//  do stuff
}
Zoredache
A: 

PHP arrays have internal pointers, so try this:

foreach($array as $key => $value){

$index = current($array); }

Works okay for me (only very preliminarily tested though).

Sorry!I was looking in the wrong place... current() doesn't work at all.