PHP/MySQL group results by column
in order to keep as few SQL statements as possible, I want to do select set from MySQL: SELECT * FROM products WHERE category IN (10,120,150,500) ORDER BY category,id; Now, I have list of products in following manner: CATEGORY - product 1 - product 2 CATEGORY 2 - product 37 ... What's the best and most efficent way to process MySQL result? I thought something like (pseudo PHP) foreach ($product = fetch__assoc($result)){ $products[$category][] = $product; } and then when outputting it, do foreach loop: foreach($categories as $category){ foreach($products[$category] as $product){ $output; } } Is