0

I am trying to merge 2 arrays: 1 multidimensional and another one normal:

Multidimensional array - $_SESSION["products"]

array (size=2)
  0 => 
    array (size=4)
      'name' => string 'Lg Monitor' (length=10)
      'code' => string '30' (length=2)
      'qty' => string '1' (length=1)
      'price' => string '1300.50' (length=7)
  1 => 
    array (size=4)
      'name' => string 'Smasung Monitor' (length=15)
      'code' => string '29' (length=2)
      'qty' => string '1' (length=1)
      'price' => string '2300.50' (length=7)

Simple array - $qty

array (size=2)
  0 => string '2' (length=1)
  1 => string '3' (length=1)

EXPECTED OUTPUT

array (size=2)
  0 => 
    array (size=4)
      'name' => string 'Lg Monitor' (length=10)
      'code' => string '30' (length=2)
      'qty' => string '2' (length=1) // notice the qty change
      'price' => string '1300.50' (length=7)
  1 => 
    array (size=4)
      'name' => string 'Smasung Monitor' (length=15)
      'code' => string '29' (length=2)
      'qty' => string '3' (length=1) // notice the qty change
      'price' => string '2300.50' (length=7)

I tried:

foreach ($_SESSION["products"] as $cart_itm){
    foreach($qty as $qt) {
        $cart_itm['qty'] = $qt;
    }
 }

But did not work, cart_itm['qty'] remained the same (1).

1 Answer 1

1

Try with this:

foreach ($_SESSION["products"] as $key => &$cart_itm){
    $cart_itm['qty'] = $qty[$key];
}
Sign up to request clarification or add additional context in comments.

1 Comment

still remaining the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.