Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is it possible to "group" array elements? 1

Status
Not open for further replies.

JohnnyT

Programmer
Jul 18, 2001
167
GB
Hi,

I've got a script that lists the elements in an array (a shopping cart):
foreach($cart as $cart_item) {
$product_id = $cart_item->product_id;
$category = $cart_item->category;
etc etc
}

The above code allows me to index through the array but I wondered if there is a way of 'ordering' the results. I want to group the results by category.

So list all the elements from the array but, instead of them being listed in the order they are held in the array, I want to list them grouped together by category.

I hope I've made myself clear enough for you to help me. If you need more information then please let me know.

Many thanks if you can shed any light on how to go about this (or even if it is possible).

Cheers

John ;-)



I don't make mistakes, I'm merely beta-testing life.
 
the items look to be in an object, not an array.

but assuming that's an oversight, are we not talking just about how to structure an array?

Code:
$categoryOrder = array();
foreach($cart as $c) {
  $categoryOrder[$c->category][] = $c;
}
pecho "<pre> ". print_r($categoryOrder, true) . "</pre>";
 
Hi jpadie,
Sheesh! You do some great work on these forums. That's about the last 4 or 5 questions that you've answered for me. Your efforts are really appreciated. ;-)

I'm not sure whether its an object or array as such. The $cart is from a session variable $_SESSION['shopping_cart'] that holds all the info on what someone has bought.

I want to be able to list the contents of the cart in category order. So the cart could have:
Object 1 is category 8
Object 2 is category 4
Object 3 is category 8
Object 4 is category 1

At the moment if I do a "foreach" loop it will just print the objects out as above.

I would like to be able to print them:
Object 4 (cat 1)
Object 2 (cat 4)
Object 1 (cat 8)
Object 3 (cat 8)

I'm not sure what your code is going to do without trying it (I won't be able to try it out until tomorrow).

Will your code do something like the above?

Many thanks again for all your help

Cheers

John ;-)

I don't make mistakes, I'm merely beta-testing life.
 
Hi jpadie,

I've just tried that code you wrote and its working like a treat.

Many many thanks once again for all your hard work.

Cheers

John ;-)

I don't make mistakes, I'm merely beta-testing life.
 
assuming these items come from a database, it might be (considerably) better to do the sorting at the database level. Just a thought.
 
Hi jpadie,

They don't come from a database. I would have been able to do the "group by" thing if they did. The GROUP BY was basically what I was looking to do but in php.

Thank you

John ;-)

I don't make mistakes, I'm merely beta-testing life.
 
that's odd. does your shopping cart software not store its data in a relational database?
 
Only at the end of the transaction. Just before you go to the purchase screen then it moves everything into a table in the db. Up until that point it is just stored in a Session variable.

I'm changing the way the shopping cart works slightly (due to my particular application) and need to be able to list all the items someone has selected in order of category rather than just the order they clicked them.

;-)

I don't make mistakes, I'm merely beta-testing life.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top