How to find the foreach index

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 an index.

Is there a need to use a for loop, or is there a way to get the index in a foreach ?

+439
loops php foreach
Sep 26 '08 at 18:23
source share
12 answers
 foreach($array as $key=>$value) { // do stuff } 

$key is the index of each element of $array

+814
Sep 26 '08 at 18:24
source share

You can put the hack in your foreach , for example, a field that grows with each pass, which exactly corresponds to the for loop in a numerically indexed array. Such a field will be a pseudo-index, which requires manual control (increments, etc.).

A foreach will give you your index as your $key value, so no such hack is needed.

e.g. in 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++; } 
+154
Sep 26 '08 at 18:25
source share

It should be noted that you can call key() in any array to find the current key. As you can guess, current() will return the current value, and next() move the array pointer to the next element.

+24
Mar 04 '11 at 11:17
source share

Owen has a good answer. If you only need a key and you are working with an array, this can also be useful.

 foreach(array_keys($array) as $key) { // do stuff } 
+18
Sep 26 '08 at 21:35
source share

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

+14
Sep 26 '08 at 18:25
source share

These two loops are equivalent (e.g. safety railing):

 for ($i=0; $i<count($things); $i++) { ... } foreach ($things as $i=>$thing) { ... } 

eg,

 for ($i=0; $i<count($things); $i++) { echo "Thing ".$i." is ".$things[$i]; } foreach ($things as $i=>$thing) { echo "Thing ".$i." is ".$thing; } 
+8
Jan 27 2018-11-23T00:
source share

I think the best option is the same:

 foreach ($lists as $key=>$value) { echo $key+1; } 

easy and usual

+6
May 12 '16 at 1:59
source share

PHP arrays have internal pointers, so try the following:

 foreach($array as $key => $value){ $index = current($array); } 

Works well for me (only very preliminary tested).

+5
Oct 28 '08 at 20:43
source share
Jonathan is right. PHP arrays act like map maps to values. in some cases, you can get the index if your array is defined, e.g.
 $var = array(2,5); for ($i = 0; $i < count($var); $i++) { echo $var[$i]."\n"; } 

your conclusion will be

 2 5 

and in this case, each element of the array has a knowing 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 have no way out. This is because arrays in PHP are not linear structures, as in most languages. They are more like hash tables, which may or may not have keys for all stored values. Consequently, foreach does not use indexes to traverse 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 going over them and use a for loop.

+4
Sep 26 '08 at 18:47
source share

I usually do this when dealing with associative arrays:

 foreach ($assoc_array as $key => $value) { //do something } 

This will work just fine with non-associative arrays. $ key will be the index value. If you prefer, you can also do this:

 foreach ($array as $indx => $value) { //do something } 
+2
Jul 07 '14 at
source share
 foreach(array_keys($array) as $key) { // do stuff } 
0
Jun 16 '16 at 10:12
source share
 foreach($arrvariable as $key=>$value){ echo "$key"; } 
-3
Sep 19 '16 at 18:02
source share



All Articles