2016-07-27 43 views
-2

下面是我有的數組的示例。如何獲得在foreach循環中的數組項的關鍵在php

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))) 

然後,我把這個數組放在foreach循環中。

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

那麼我怎樣才能得到循環內的項目的關鍵?

請幫幫我。提前致謝。

+0

去看看'的foreach()'手冊頁 – 2016-07-27 08:10:00

回答

2

你只需要指定一個變量的關鍵存儲爲您foreach迴路設置的一部分。你可以是這樣做的:

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

爲清楚起見,您可以撥打$key變量任何你喜歡的。它不一定是$key


相關閱讀:

-1
foreach($mainArray as $k => $item){ 
    $k is the key 
    $item is the value 
} 
0

利用這一點,

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

} 
相關問題