forgot to add
reset($sessarray);
before each of the while() loops
Main Topics
Browse All Topicsok, Arrays
for some reason arrays have always got the better of me so I normally come up with away around using them. Unfortunatley I am up against a problem and the only solution I can see is to use an array.
So
I need to keep a seperate track of products and categories that a shopper has in their cart.
I need to use a session variable to do it.
and, because I have no idea how many products or categories there will be, I need to use an array.
can I have something like
$_SESSION['cat_check'] = array(); ?
each part of the array has an ID number (i assume), the category id then the product id?
can I check for the existance of a product id (how) if its not there how do I add the category id and product id to the end of the array?
also what I want is a product id once but that may lead to a category id being in more than once - can I check how many times a category id is in there?
If it needs more points then I'm happy to sort that.
Just to clarify, I don't want to/can't use a database table so please don't suggest that as a possible solution :)
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
TheClickMaster's solution is what you want. A couple of things:
instead of array_push, you can use:
$sessarray[] = $item
You don't have the overhead of calling a function
(ref: http://us3.php.net/manual/
Also, you can use the in_array function to do some basic searching.
$product = 'AX111';
$found = in_array($product,$sessarr
The online PHP manual is one of the greatest resources for PHP out there.
http://www.php.net/manual/
Take the time to learn arrays. Once you do, you'll always be looking for ways to use them in the problems you face.
Pete Hanson
Up And Running
Business Accounts
Answer for Membership
by: TheClickMasterPosted on 2005-10-12 at 09:43:25ID: 15070529
// reference to $session['cat_check'], saves some typing
$sessarray = &$session['cat_check'];
// for each item you want to add create an array with the cat Id and prod Id
$item = array("category","producti
// add items to the session
array_push($sessarray, $item);
// check if a product id exists
$product = 'AX111';
$found = false;
while((list(, $item) = each($sessarray)) && $found != true)
{
// $item[0] is the category, $item[1] is the product id
if($item[1] == $product)
{
$found = true;
}
}
// count the number of times a category is there
$category = 'CAT1';
$found = 0; // $found contains the # of times the category was found
while(list(, $item) = each($sessarray))
{
// $item[0] is the category, $item[1] is the product id
if($item[0] == $category)
{
$found += 1;
}
}