I have 2 arrays - one is hard coded and the other is data retrieved from a database. I am trying to merge them but I'm having unexpected results.
This is the first array:
$base_image_array = [
["product_image_one" => ""],
["product_image_two" => ""],
["product_image_three" => ""],
["product_image_four" => ""]
];
This is the second array:
$db_product_images = [
["product_image_one" => "../wp-content/themes/dosco/images/products_images/355_product_image_one.jpg"],
];
However, when I try array_merge($base_image_array, $db_product_images)
, I get 5 rows and the produce_image_one
occurs more than once.
What I want to achieve is this:
[
['product_image_one' => '../wp-content/themes/dosco/images/products_images/355_product_image_one.jpg'],
['product_image_two' => ''],
['product_image_three' => ''],
['product_image_four' => '']
]
I think the multidimensional nature of the arrays is confusing me.
The problem is that the array keys in both arrays are numeric. If you want one array to overwrite the other you need to get them into a state where the array keys are strings. You could do something like this:
There are probably better ways to loop through this with the php array functions - but this should get the job done!