我在使用該函數處理數組時遇到了一些問題array_merge。這是一個例子:$first_array = [ 8 => [ 'name' => "Hershey's Chocolate Milk Shake", 'code' => 8 , 'price' => 29.00, 'quantity' => 1, 'image' => "Hersheys_Chocolate_Milk_Shake.jpg", 'percentage_discount' => 0, 'offer_mrp' => 0, ]];$second_array = [ 20 => [ 'name' => 'Kissan Mixed Fruit Jam 700g', 'code' => 20, 'price' => 215.00, 'quantity' => 1, 'image' => 'Kissan Mixed Fruit Jam 700g.jpg', 'percentage_discount' => 0, 'offer_mrp' => 0 ]];$first_array = array_merge($first_array, $second_array); print_r($first_array);結果是:Array ( [0] => Array ( [name] => Hershey's Chocolate Milk Shake [code] => 8 [price] => 29.00 [quantity] => 1 [image] => Hersheys_Chocolate_Milk_Shake.jpg [percentage_discount] => 0 [offer_mrp] => 0 ) [1] => Array ( [name] => Kissan Mixed Fruit Jam 700g [code] => 20 [price] => 215.00 [quantity] => 1 [image] => Kissan Mixed Fruit Jam 700g.jpg [percentage_discount] => 0 [offer_mrp] => 0 ) );但我希望它是:Array ( [8] => Array ( [name] => Hershey's Chocolate Milk Shake [code] => 8 [price] => 29.00 [quantity] => 1 [image] => Hersheys_Chocolate_Milk_Shake.jpg [percentage_discount] => 0 [offer_mrp] => 0 ) [20] => Array ( [name] => Kissan Mixed Fruit Jam 700g [code] => 20 [price] => 215.00 [quantity] => 1 [image] => Kissan Mixed Fruit Jam 700g.jpg [percentage_discount] => 0 [offer_mrp] => 0 ))
1 回答

犯罪嫌疑人X
TA貢獻2080條經驗 獲得超4個贊
array_merge()
重新枚舉數字鍵。您應該改用運算+
符。
$first_array = $first_array + $second_array;
輸出與您想要的完全相同。
- 1 回答
- 0 關注
- 142 瀏覽
添加回答
舉報
0/150
提交
取消