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++;
}
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.
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.
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#? 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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With