Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 0.94 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. PHP read from group array
  2. +---------------+-----------------+
  3. | Type          |  Price          |
  4. +---------------+-----------------+
  5. | Music         |  19.99          |
  6. | Music         |   3.99          |
  7. | Music         |  21.55          |
  8. | Toy           |  89.95          |
  9. | Toy           |   3.99          |
  10. | Phones        |  99.99          |
  11. | Phones        |  89.99          |
  12. +---------------+-----------------+
  13.        
  14. // Get the sum
  15. $group = array();
  16. foreach($rows as $r){
  17.   $group[$r->type] = $group[$r->Type] + $r->Price;
  18. }
  19.  
  20. // Display
  21. foreach($group as $type=>$price){
  22.   echo "Type:".$type." Price: ".$price;
  23. }
  24.        
  25. Music  | 45.53
  26. Toy    | 93.94
  27. Phones | 189.98
  28.        
  29. //this will create new array without first element
  30. $new_group = array_slice($group, 1);
  31.        
  32. $first = true;
  33. foreach ($group as $type=>$price) {
  34.     if ($first) {
  35.         $first = false;
  36.         continue;
  37.     }
  38.     echo "Type:", $type, " Price: ", $price;
  39. }
  40.        
  41. echo $group['Music'];