Sum of multidimensional array values in PHP

Asked

Viewed 2,919 times

0

I am building a shopping cart and I am using Sessions to save the products from the cart. The array structure that saves items is as follows:

$_SESSION['carrinho'][ID_DO_PRODUTO;TAMANHO_SE_HOUVER] => QUANTIDADE

An example of items in the shopping cart would be:

$_SESSION['carrinho']['156;GG'] => 1,
$_SESSION['carrinho']['876;PP'] => 9,
$_SESSION['carrinho']['65;'] => 5,

The last item in the list above is one size only, while the others are GG and PP respectively. However I need to add the total number of items in the cart. I know the array_sum, but the indexes of the array are dynamic. I’ve thought about using a foreach, but to me it looks uglier than my gabiarra implementation.

  • 1

    Hello friend, it is not an answer to your question, but for Shopping Carts the best option is always to work with Cookies, transfer this storage to the Customer and remove it from the server. You continue to work normally with Array. Hugs.

  • I understand friend, in this case would only change from SESSION to COOKIE. I will see this change.

  • 3

    I do not entirely agree with @Marcelobarbosa, cookies should be used if Ecommerce has no authentication system ("user account"), the data of cookies are available in the customer, which increases the chances of modifying the data and thus "hacking" your site. Already Session is on the server side, so only those who have access to the './tmp' folder can edit... [continue to the next comment]

  • [continuing]...the only drawback is that if one closes the browser Session may expire, but it is possible to bypass this. IF YOUR ECOMMERCE have to register after making the first cart, so it is best to save in Sesssion and after authenticating or creating an account, this be MOVED to a database.

  • @Guilhermenascimento Storing Code and quantity in a Cookie allows someone to "hack"? At most, if you are lucky, change the products of the cart. Shopping cart is just a temporary container for checkout, in which you will need a login and then yes to BD storage. Whenever possible remove useless things from the server, do, for example "Shopping Cart", there is no need to store this information on "Server-side" taking into account whether it is manually in the cookie or directly on the website, the user has authorization to change its content.

  • 1

    @Marcelobarbosa if it is temporary then use Session, and about preventing the session to expire when closing the browser is as I said, there is solution to circumvent this.

Show 1 more comment

2 answers

1


I believe that a foreach be the simplest and cleanest solution to your problem:

$totalItens = 0;
foreach ($_SESSION['carrinho'] as $itemID => $itemQTD) {
  $totalItens += $itemQTD;
}

echo $totalItens . ' itens no carrinho';

The use of foreach is perfectly acceptable when there will be read-only array.

0

Beyond the foreach, it is possible to use a closure (anonymous function) to sum:

$contagemTotal = array_reduce($produtos['carrinho'], function($contagemTotal, $contagem) {
    return $contagemTotal += $contagem;
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.