Posted By

orazal on 01/29/15


Tagged


Versions (?)

Multidimensional array


 / Published in: PHP
 

Loop through JSON and group values according to category so as to print out a TH for each category with rows underneath. If there is only one value for a category, print the header and value in the same row.

  1. // Loop through data and store in arrays grouped by occupancy
  2. $ratesArray = array();
  3. foreach($rates->rates as $rate){
  4. // check if "row" already exists and append rate to it
  5. if($ratesArray[$rate->occupancy]){
  6. array_push($ratesArray[$rate->occupancy], $rate);
  7. }else{
  8. // Otherwise add new row with rate
  9. $ratesArray[$rate->occupancy] = array($rate);
  10. }
  11. }
  12. // Loop through multidimensional array
  13. echo "<!-- RATES PRINTOUT \n";
  14. foreach($ratesArray as $x => $x_value) {
  15.  
  16. echo "Header" . $x ;
  17. if(count($x_value) == 1){
  18. echo " - > " . $x_value[0]->price;
  19. echo "\n";
  20. }else{
  21. echo "\n";
  22. foreach($x_value as $y => $y_value){
  23. echo " ->" . $y_value->type . " " . $y_value->price;
  24. }
  25. echo "\n";
  26. }
  27.  
  28. }

Report this snippet  

You need to login to post a comment.