我有以下代碼片段。foreach訪問索引或關聯數組
$items['A'] = "Test";
$items['B'] = "Test";
$items['C'] = "Test";
$items['D'] = "Test";
$index = 0;
foreach($items as $key => $value)
{
echo "$index is a $key containing $value\n";
$index++;
}
預期輸出:
0 is a A containing Test
1 is a B containing Test
2 is a C containing Test
3 is a D containing Test
有沒有辦法離開了$index
變量?
其實它是索引,A,B,C和D是數組鍵。 – 2008-09-16 01:43:26
但是你對數據重構是正確的,你的例子幾乎就是我最終的結果。 :) – 2008-09-16 01:44:17