Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Array index in foreach

Tags:

php

I have a foreach loop in php to iterate an associative array. Within the loop, instead of increamenting a variable I want to get numeric index of current element. Is it possible.

$arr = array('name'=>'My name','creditcard'=>'234343435355','ssn'=>1450);
foreach($arr as $person){
  // want index here
}

I usually do this to get index:

$arr = array('name'=>'My name','creditcard'=>'234343435355','ssn'=>1450);
    $counter =0;
    foreach($arr as $person){
      // do a stuff
$counter++;
    }
like image 638
Shahid Karimi Avatar asked Nov 01 '11 13:11

Shahid Karimi


People also ask

Can I get index in forEach?

You can use the regular foreach construct and be able to access the value and index directly, not as a member of an object, and keeps both fields only in the scope of the loop.

How do you find the index of an array?

To find the position of an element in an array, you use the indexOf() method. This method returns the index of the first occurrence the element that you want to find, or -1 if the element is not found. The following illustrates the syntax of the indexOf() method.

What is index in forEach JavaScript?

Get the Current Array Index in JavaScript's forEach() It is essentially a class that encapsulates an array (an ordered list of values) and all necessary methods you might need to perform array operations. The fact every array is ordered means that the place of each element is of great importance.

Can we obtain the array index using forEach loop in C#?

Can we obtain the array index using foreach loop in C#? Explanation: No, we cannot obtain the array index using foreach loop because it does not keep track of the index, it only iterates forward over the array in single steps.


2 Answers

Use this syntax to foreach to access the key (as $index) and the value (as $person)

foreach ($arr as $index => $person) {
   echo "$index = $person";
}

This is explained in the PHP foreach documentation.

like image 158
Michael Berkowski Avatar answered Oct 23 '22 13:10

Michael Berkowski


Why would you need a numeric index inside associative array? Associative array maps arbitrary values to arbitrary values, like in your example, strings to strings and numbers:

$assoc = [
    'name'=>'My name',
    'creditcard'=>'234343435355',
    'ssn'=>1450
];

Numeric arrays map consecutive numbers to arbitrary values. In your example if we remove all string keys, numbering will be like this:

$numb = [
    0=>'My name',
    1=>'234343435355',
    2=>1450
];

In PHP you don't have to specify keys in this case, it generates them itself. Now, to get keys in foreach statement, you use the following form, like @MichaelBerkowski already shown you:

foreach ($arr as $index => $value) 

If you iterate over numbered array, $index will have number values. If you iterate over assoc array, it'll have values of keys you specified.

Seriously, why I am even describing all that?! It's common knowledge straight from the manual!

Now, if you have an associative array with some arbitrary keys and you must know numbered position of the values and you don't care about keys, you can iterate over result of array_values on your array:

foreach (array_values($assoc) as $value) // etc

But if you do care about keys, you have to use additional counter, like you shown yourself:

$counter = 0;
foreach ($assoc as $key => $value)
{
    // do stuff with $key and $value
    ++$counter;
}

Or some screwed functional-style stuff with array_reduce, doesn't matter.

like image 37
hijarian Avatar answered Oct 23 '22 12:10

hijarian