-2

Below is the example of the array that I have.

  Array (
[952] => Array ( [Date] => 2016-06-23 01:55:17 [SValues] => Array ( [total] => 1 [Name] => Name [OverAge] => No))
[91] => Array ( [Date] => 2016-06-23 01:55:17 [SValues] => Array ( [total] => 1  [Name] => Name [OverAge] => No))
[83] => Array ( [Date] => 2016-06-23 01:55:17 [SValues] => Array ( [total] => 1 [Name] => Name [OverAge] => No)))

And then, I put this array inside the foreach loop.

foreach($the-main-array as $item)
{
          //I want to get the key of the item here (952,91,83)
}

So how can I get the key of the item inside the loop?

Please help me. Thanks in advance.

Mg Thar
  • 1,084
  • 8
  • 24

3 Answers3

2

You just need to specify a variable to store the key in as part of your foreach loop setup. You can do that like this:

foreach($the-main-array as $key => $item){
    echo "This is the key: ".$key;
}

For clarity, you can call the $key variable anything you like. It doesn't have to be $key.


Related reading:

Henders
  • 1,195
  • 1
  • 21
  • 27
0

Use This,

foreach($the-main-array as $key => $item)
{
// Put your code here.
// $key have the key value.

}
Avishake
  • 460
  • 1
  • 6
  • 20
-1
foreach($mainArray as $k => $item){
    $k is the key
    $item is the value
}
BeetleJuice
  • 39,516
  • 19
  • 105
  • 165