我想讓我的頁面上顯示數組的所有鍵。PHP顯示數組鍵1個數字更高
現在我有這樣的:
$subjectcodes[1] = "Mathematics";
$subjectcodes[2] = "Physics";
$subjectcodes[3] = "Charlie";
$subjectcodes[4] = "Chemistry";
$subjectcodes[5] = "Biology";
$subjectcodes[6] = "English";
$subjectcodes[7] = "Dutch";
$subjectcodes[8] = "German";
$subjectcodes[9] = "Sociology";
$subjectcodes[10] = "Physical Education";
$subjectcodes[11] = "Art";
$subjectcodes[12] = "General Science";
$subjectcodes[13] = "Philosophy";
$subjectcodes[14] = "Management and Organization";
$subjectcodes[15] = "Research and Design";
foreach ($subjectcodes as &$value) {
$key = key($subjectcodes);
echo "<option value=" . $key . ">" . $value . "</option>";
}
當我去我的頁面代碼,我得到:
<option value=2>Mathematics</option>
<option value=3>Physics</option>
<option value=4>Charlie</option>
<option value=5>Chemistry</option>
<option value=6>Biology</option>
<option value=7>English</option>
<option value=8>Dutch</option>
<option value=9>German</option>
<option value=10>Sociology</option>
<option value=11>Physical Education</option>
<option value=12>Art</option>
<option value=13>General Science</option>
<option value=14>Philosophy</option>
<option value=15>Management and Organization</option>
<option value=>Research and Design</option></select>
正如你所看到的所有按鍵均高1號轉念一想。最後一個選項甚至沒有鑰匙...
有沒有人爲什麼這是,我怎麼能解決這個問題?
謝謝!
這是寫一個foreach的奇怪方法! – Yousf
@Yousf你能否說出原因? – Ahmed
選擇foreach循環變量作爲參考,並從循環中獲取密鑰。標準的方式是@Niloy Shaa說。 'foreach($ my_array as $ key => $ value)' – Yousf