4

I am storing shopping cart data in a SESSION Array like this:

$_SESSION['cart'][$sessID] = array ('quantity' => 1, 'price' => $prodPrice, 'prodName' => $prodName, 'size' => $size, 'handle' => $handle)

Each time a user adds an item to the cart, a new sessID is created and a new Session Array.

How do I count how many sessID's there are when it comes to checkout?

I don;t want to count the items in the shopping cart - I want to count the number of occurances of $_SESSION['cart']

Thank you

1
  • If any of the answers here solved your problem, you should accept it by clicking on the check mark below the answers number of votes. Others will then know that this issue has been closed. Commented Oct 4, 2009 at 11:03

2 Answers 2

7

If I understand the question correctly you're looking for count()

count($_SESSION['cart'])
Sign up to request clarification or add additional context in comments.

1 Comment

This works perfectly - thanks! I tried this before, but for some reason I used something like: count($_SESSION['cart']['sessID']) so I got the number of items in each array, rather than the number of arrays in the Session. Thanks again - I appreciate it
5

If you are sure $_SESSION['cart'] contains something, you can use:

$items_in_cart = count($_SESSION['cart'])

If it can be empty:

$items_in_cart = is_array($_SESSION['cart']) ? count($_SESSION['cart']) : 0

1 Comment

This is correct but you should change the variable name to something other than $items_in_cart, because the content is the number of $sessID in cart. For the number of items you had to sum up the quantities.

Your Answer

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