Advertisement
Guest User

Exam 22dec Gosho is moving

a guest
May 2nd, 2015
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.41 KB | None | 0 0
  1. <?php
  2.  
  3. $_GET['luggage']= 'furniture;living room;pink couch;40.85kgC|_|furniture;bedroom;night table;
  4. 5.12kgC|_|boxes;kitchen;plates;10.36kgC|_|boxes;kitchen;cups;10.36kgC|_|boxes;kitchen;tableware;
  5. 7.6kgC|_|boxes;living room;glasses;3.32kgC|_|boxes;living room;dresses;4.32kgC|_|bags;hall;shoes;5.9kgC|_|';
  6.  
  7. $_GET['typeLuggage'] = ['furniture', 'boxes', 'bags'];
  8.  
  9. $_GET['room'] = 'living room';
  10.  
  11. $_GET['minWeight'] = '5';
  12. $_GET['maxWeight'] = '50';
  13.  
  14. //SOLUTION STARTS FROM HERE
  15. $luggage = array_filter(explode('C|_|', trim($_GET['luggage'])));
  16.  
  17. $lugTypes = $_GET['typeLuggage'];
  18. $room = $_GET['room'];
  19. $minWeight =$_GET['minWeight'] = '5';
  20. $maxWeight = $_GET['maxWeight'] = '50';
  21. $lugPieces = array();
  22. foreach ($luggage as $piece) {
  23.     $piece = explode(';', $piece);
  24.     $type = trim($piece[0]);
  25.     $place = trim($piece[1]);
  26.     $item = trim($piece[2]);
  27.     $weight = intval($piece[3]);
  28.     if (!array_key_exists($type, $lugPieces)) {
  29.         $lugPieces[$type] = array();
  30.     }
  31.     if (!array_key_exists($place, $lugPieces[$type])) {
  32.         $lugPieces[$type][$place] = array();
  33.         $lugPieces[$type][$place]['weight'] = $weight;
  34.     } else {
  35.         $lugPieces[$type][$place]['weight'] += $weight;
  36.     }
  37.     $lugPieces[$type][$place]['items'][] = $item;
  38.  
  39.  
  40.  
  41. }
  42.  
  43. $lugPieces = array_filter($lugPieces, function($v, $k) use ($lugTypes){
  44.    return in_array($k, $lugTypes);
  45. }, 1);
  46.  
  47. foreach ($lugPieces as $key => $piece) {
  48.     $lugPieces[$key] = array_filter($piece, function($v, $k) use ($room, $minWeight, $maxWeight){
  49.         return $k === $room && ($v['weight'] >= $minWeight && $v['weight'] <= $maxWeight);
  50.     }, 1);
  51.  
  52. }
  53. $lugPieces = array_filter($lugPieces);
  54.  
  55. ksort($lugPieces);
  56. foreach ($lugPieces as $key => $piece) {
  57.     ksort($piece);
  58.     foreach ($piece as $k => $lugRoom) {
  59.         sort($lugRoom['items']);
  60.         $piece[$k] = $lugRoom;
  61.     }
  62.     $lugPieces[$key] = $piece;
  63. }
  64.  
  65.  
  66. echo '<ul>';
  67. foreach ($lugPieces as $type => $item) {
  68.     echo '<li><p>'.htmlspecialchars($type).'</p><ul>';
  69.     foreach ($item as $kroom => $room) {
  70.         echo '<li><p>'.htmlspecialchars($kroom).'</p>';
  71.         foreach ($room['items'] as $k => $item) {
  72.             $room['items'][$k] = htmlspecialchars($item);
  73.         }
  74.         $pieces = implode(', ', $room['items']);
  75.         echo '<ul><li><p>'.$pieces.' - '. $room['weight'] .'kg</p></li></ul></li>';
  76.     }
  77.     echo '</ul></li>';
  78. }
  79. echo '</ul>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement