PHP   55

menuitems

Guest on 20th June 2022 04:49:31 PM

  1. <?php
  2.  
  3. $domDoc = domxml_open_file('data.xml');
  4.  
  5. $runs = $domDoc->get_elements_by_tagname('Run');
  6.  
  7. $menuitems = array();
  8.  
  9.  
  10. $count = 0;
  11. foreach($runs as $run) {
  12.   $runitems = array();
  13.   $starttime = $run->get_elements_by_tagname('StartTime');
  14.  
  15.   $timestr = str_replace("T", " ", $starttime[0]->get_content());
  16.   $time = strtotime($timestr);
  17.   $runitems['time'] = date("M j, Y g:i a", $time);
  18.  
  19.   $duration = $run->get_elements_by_tagname('Duration');
  20.   $duration = $duration[0]->get_content();
  21.   $duration = str_replace("PT", "", $duration);
  22.   $duration = str_replace("S", "", $duration);
  23.   $duration = date("H:i:s", $duration - date("Z"));  
  24.  
  25.   $runitems['duration'] = $duration;
  26.  
  27.   $length = $run->get_elements_by_tagname('Length');
  28.   $runitems['length'] = number_format($length[0]->get_content() /
  29. 1609.344, 2) . " miles";
  30.   $runitems['count'] = $count++;
  31.   array_push($menuitems, $runitems);
  32. }
  33.  
  34. echo "[";
  35. $first_menu = 0;
  36. foreach ($menuitems as $item) {
  37.   if ($first_menu) echo ",\n";
  38.   echo "{\n";
  39.   $first_item = 0;
  40.   foreach($item as $key => $val) {
  41.     if ($first_item) echo ",\n";
  42.     echo "\"$key\": ";
  43.  
  44.     if (is_numeric($val)) {
  45.       echo $val;
  46.     } else {
  47.       echo "\"$val\"";
  48.     }
  49.  
  50.     $first_item = 1;
  51.   }
  52.   echo "}\n";
  53.   $first_menu = 1;  
  54. }
  55. echo "]";
  56.  
  57.  
  58. ?>

Raw Paste


Login or Register to edit or fork this paste. It's free.