我需要將更多的鍵和它的值推入數組中。如果我使用下面的代碼第一個密鑰對替換爲第二個密鑰對。需要推關聯數組內的鍵和值?
,供大家參考:
代碼中使用:
foreach ($projectData['projectsections'] as $key => $name) {
$projectData['projectsections'][$key] = ['name' => $name];
$projectData['projectsections'][$key]= ['id' => '1'];
}
當前的結果:
'projectsections' => [
(int) 0 => [
'id' => '1'
],
(int) 1 => [
'id' => '1'
]
],
預計:
'projectsections' => [
(int) 0 => [
'name' => 'test1',
'id' => '1'
],
(int) 1 => [
'name' => 'test2',
'id' => '1'
]
],
我該如何在PHP中構建這個數組?任何一個幫助?
你重寫你的數組與你的第三條線,只是結合第2和第3行 – ctwheels