Diether Posted March 12, 2013 Share Posted March 12, 2013 hi Guys Good day.. i have this two array that i want to combine:1st array:when using print_r($productItems)Here is the result:Array( [0] => Array ( [item_id] => 13 [quantity] => 4 ) [1] => Array ( [item_id] => 15 [quantity] => 1 ) [2] => Array ( [pricetotal] => 3709 ))2nd array:When using print_r($prices)Here is the result:Array( [price] => 3,200.00)Array( [price] => 509.00)By using the array_merge, i came up with a solution:Here is what i did: $result = array_merge($productItems, $prices); print_r($result); Here is the output:Array( [0] => Array ( [item_id] => 13 [quantity] => 4 ) [1] => Array ( [item_id] => 15 [quantity] => 1 ) [2] => Array ( [pricetotal] => 3709 ) [price] => 509.00)but thats not the result i want to get :this is my expected output:Array( [0] => Array ( [item_id] => 13 [quantity] => 4 [price] => 3,200.00 ) [1] => Array ( [item_id] => 15 [quantity] => 1 [price] => 509.00 ) [2] => Array ( [pricetotal] => 3709 ) )please help me to do this,, im stuck for almost 3 hours already thats why i seek for help. thanks Quote Link to comment https://forums.phpfreaks.com/topic/275545-helpcombining-two-arrays/ Share on other sites More sharing options...
AyKay47 Posted March 12, 2013 Share Posted March 12, 2013 I doubt that the second output that you posted is one single array, as it looks like the output of 2 separate one-dimensional arrays. The below code will assume that the $prices array is a two-dimensional array like $productItems. If it's not, the code will need adjusted: $productItems = array(array('item_id' => 13, 'quantity' => 4), array('item_id' => 15, 'quantity' => 1), array('pricetotal' => 3709)); $prices = array(array('price' => '3,200.00'), array('price' => '509.00')); for($i=0; $i < count($prices); $i++) { $productItems[$i]['price'] = $prices[$i]['price']; } print_r($productItems); Output: Array ( [0] => Array ( [item_id] => 13 [quantity] => 4 [price] => 3,200.00 ) [1] => Array ( [item_id] => 15 [quantity] => 1 [price] => 509.00 ) [2] => Array ( [pricetotal] => 3709 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/275545-helpcombining-two-arrays/#findComment-1418163 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.