A
A
Alexander Kudryashkin2020-04-30 15:32:28
PHP
Alexander Kudryashkin, 2020-04-30 15:32:28

Calculate the sum of elements in a quantity field / PHP?

You need to create a function called countBasket, with one parameter - an array of positions in the basket.

The function should calculate and return how many items are in the cart (there can be several items in one item of the cart - the quantity field).

An array of the following structure will be passed to your function:

$basket = [
    [
        'position' => 'Book',
        'quantity' => 1,
    ],
    [
        'position' => 'Mouse',
        'quantity' => 12,
    ],
 ]

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2020-04-30
@kuudr

array_sum(array_column($basket, 'quantity'))

V
Vitaliy K, 2020-04-30
@revenger

Count number of elements in PHP array

Come on.
sizeof(Array)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question