我在PHP中有一個foreach循環來迭代關聯數組。在循環內,而不是增加一個變量,我想獲取當前元素的數字索引。可能嗎。如何在foreach中獲取數組索引
$arr = array('name'=>'My name','creditcard'=>'234343435355','ssn'=>1450);
foreach($arr as $person){
// want index here
}
我通常這樣做是爲了獲得指數:
$arr = array('name'=>'My name','creditcard'=>'234343435355','ssn'=>1450);
$counter =0;
foreach($arr as $person){
// do a stuff
$counter++;
}
關聯數組有什麼用途? – Sonny