
Untitled
By: a guest on
May 8th, 2012 | syntax:
None | size: 0.94 KB | hits: 12 | expires: Never
PHP read from group array
+---------------+-----------------+
| Type | Price |
+---------------+-----------------+
| Music | 19.99 |
| Music | 3.99 |
| Music | 21.55 |
| Toy | 89.95 |
| Toy | 3.99 |
| Phones | 99.99 |
| Phones | 89.99 |
+---------------+-----------------+
// Get the sum
$group = array();
foreach($rows as $r){
$group[$r->type] = $group[$r->Type] + $r->Price;
}
// Display
foreach($group as $type=>$price){
echo "Type:".$type." Price: ".$price;
}
Music | 45.53
Toy | 93.94
Phones | 189.98
//this will create new array without first element
$new_group = array_slice($group, 1);
$first = true;
foreach ($group as $type=>$price) {
if ($first) {
$first = false;
continue;
}
echo "Type:", $type, " Price: ", $price;
}
echo $group['Music'];