我命名$classes_data
關聯數組如下:如何在PHP中的關聯數組中插入新的鍵值對?
Array
(
[2] => Array
(
[class_id] => 2
[class_name] => II
[subjects] => Array
(
[0] => 11 Engllish
)
)
[3] => Array
(
[class_id] => 3
[class_name] => III
[subjects] => Array
(
[0] => Hidi
[1] => 11 Maths
[2] => 11 Science
[3] => 11 Engllish
)
)
[4] => Array
(
[class_id] => 4
[class_name] => IV
[subjects] => Array
(
[0] => Physics
)
)
[6] => Array
(
[class_id] => 6
[class_name] => VI
[subjects] => Array
(
[0] => Mathematics
[1] => dfadadadsagfasrsarasrarBiology
)
)
[7] => Array
(
[class_id] => 7
[class_name] => VII
[subjects] => Array
(
[0] => Physics
[1] => Chemistry11
[2] => 11 Science
)
)
[8] => Array
(
[class_id] => 8
[class_name] => VIII
[subjects] => Array
(
[0] => Hidi
[1] => 11 Engllish
)
)
[9] => Array
(
[class_id] => 9
[class_name] => IX
[subjects] => Array
(
[0] => Mathematics
[1] => Hidi
[2] => 11 Science
)
)
)
數組的鍵(即2,3,4,6,7,8,9,10-)是在這樣的方式,而不是0,1 ,2,3,4,5,6因爲我使用了一個函數來重新排列這些鍵。
現在我想要做的是插入一個新的密鑰class_checked
並將其初始值設置爲0
(即class_checked =>"0"
)。
我嘗試了很多技巧,但無法獲得所需的陣列格式。任何人都可以幫助我獲得理想的數組?提前致謝。
爲了您的信息陣列$classes_data
所需的陣列格式如下:
Array
(
[2] => Array
(
[class_id] => 2
[class_name] => II
[class_checked] => 0
[subjects] => Array
(
[0] => 11 Engllish
)
)
[3] => Array
(
[class_id] => 3
[class_name] => III
[class_checked] => 0
[subjects] => Array
(
[0] => Hidi
[1] => 11 Maths
[2] => 11 Science
[3] => 11 Engllish
)
)
[4] => Array
(
[class_id] => 4
[class_name] => IV
[class_checked] => 0
[subjects] => Array
(
[0] => Physics
)
)
[6] => Array
(
[class_id] => 6
[class_name] => VI
[class_checked] => 0
[subjects] => Array
(
[0] => Mathematics
[1] => dfadadadsagfasrsarasrarBiology
)
)
[7] => Array
(
[class_id] => 7
[class_name] => VII
[class_checked] => 0
[subjects] => Array
(
[0] => Physics
[1] => Chemistry11
[2] => 11 Science
)
)
[8] => Array
(
[class_id] => 8
[class_name] => VIII
[class_checked] => 0
[subjects] => Array
(
[0] => Hidi
[1] => 11 Engllish
)
)
[9] => Array
(
[class_id] => 9
[class_name] => IX
[class_checked] => 0
[subjects] => Array
(
[0] => Mathematics
[1] => Hidi
[2] => 11 Science
)
)
)
謝謝Dharmesh Patel。這是我真正想要的。你的代碼對我來說真的很神奇。再次感謝您的寶貴幫助。 – PHPLover 2013-05-07 06:46:24