Reputation: 25
I have some problems during working with arrays using the array_merge
function. Here's an example:
$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);
The result is:
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
)
);
But I want it to be:
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
)
)
Upvotes: 0
Views: 87
Reputation: 10592
array_merge()
renumerates numeric keys. You should use operator +
instead.
$first_array = $first_array + $second_array;
Output is exactly the same as you want.
Upvotes: 1