0

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

Edited by pritaeas: Adjusted title.

2
Contributors
1
Reply
22
Views
4 Years
Discussion Span
Last Post by broj1
0

Provided that $result has one element less than $productItems:

foreach($productItems as $key => $item) {

    if(isset($result[$key])) {

        $productItems[$key]['price'] = $result[$key];
    }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.