views:

453

answers:

2

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

+3  A: 

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

count($_SESSION['cart'])
VolkerK
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
VCM
+4  A: 

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
Lukáš Lalinský
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.
rogeriopvl
Thanks a lot - I appreciate your help
VCM