0

I have this

        foreach ($item_id as $key => $no) {
            $input['price'] = $price[$key]['price'];
            $input['quantity'] = $quantity[$key];
            $subtotal[] = [$input['price']*$input['quantity']];
        }
        $total_price = array_sum($subtotal);

When I dd($total_price) it return 0.

When I dd($subtotal) it return my array like this

array:3 [▼
  0 => array:1 [▼
    0 => 3000000
  ]
  1 => array:1 [▼
    0 => 3
  ]
  2 => array:1 [▼
    0 => 9
  ]
]

How do I sum my array from that? Thanks!

1
  • 2
    Avoid adding a array to subtotal, e.g, $subtotal[] = $input['price']*$input['quantity'] and it should work. Commented Oct 14, 2021 at 8:32

1 Answer 1

4

In this line :

$subtotal[] = [$input['price']*$input['quantity']];

you are assigning an array, which is why you end up with the structure that you see when you dd($subtotal).

Just do :

$subtotal[] = $input['price']*$input['quantity'];

and you should be fine.

Sign up to request clarification or add additional context in comments.

Comments

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.