Jump to content

[Help]Combining two arrays


Diether

Recommended Posts

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




 
Link to comment
Share on other sites

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
        )
 
)
 
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.