Ixxi's avatar
Level 1

Merging a multi dimensional array

I'm trying to merge 2 multi dimensional arrays. The only issue I'm having is that in both arrays there is a key called "perc" and because of that when I merge them together "perc" becomes an array with both of the "perc" values.

"pricing" => array:11 [
	"local_price" => "309"
	"perc" => array:2 [
		0 => "0.084"
		1 => "0.084"
	]
	"international_price" => "312"
]

The "perc" will always be the same.

What I'm trying to do is merge the arrays together and I would like "perc" to only have the one value so the array would look like this

"pricing" => array:11 [
	"local_price" => "309"
	"perc" => "0.084"
	"international_price" => "312"
]

This is the code I've got


$localPrice = productService()->getLocalPrice($product->id);
$internationalPrice = productService()->getInternationalPrice($product->id);
$price = array_merge_recursive($localPrice, $internationalPrice);
0 likes
6 replies
saifqureshi341's avatar
$array1 = ["local_price" => "309"];
$array2 = ["perc" => ['0.084','0.084']];
$array3 = ["international_price" => "312"];
	
$array2 = array_unique($array2['perc']); 
	
$array4 = array_merge($array1,$array3);
$array4['perc'] = $array2[0];
	
print_r($array4);

Output:

Array
(
    [local_price] => 309
    [international_price] => 312
    [perc] => 0.084
)
Ixxi's avatar
Level 1

@sr57 - It didn't merge all the other info in my 2 arrays. I only got the last array

sr57's avatar

@Ixxi

Please share sample from the 2 input arrays

Ixxi's avatar
Level 1

@sr57 sure here it is Local pricing

"pricing" => [
	"local_price" => 309,
	"perc" => 0.084
],
"product_1" => [
	"local_price" => 309,
	"perc" => 0.084
]

International pricing

"pricing" => [
	"international_price" => 312,
	"perc" => 0.084
],
"product_1" => [
	"international_price" => 312,
	"perc" => 0.084
]

Please or to participate in this conversation.