Advertisement
zornitza_gencheva

04_Price_List_exam_task

Sep 3rd, 2014
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. <?php
  2. $table = $_GET['priceList'];
  3.  
  4. $matches = [];
  5. $categories = [];
  6. preg_match_all("|<td>\s*(.*?)\s*</td>\s*<td>\s*(.*?)\s*</td>\s*<td>\s*(.*?)\s*</td>\s*<td>\s*(.*?)\s*</td>|", $table, $matches, PREG_PATTERN_ORDER);
  7. unset($matches[0]);
  8.  
  9. for($j = 0 ; $j < count($matches[2]); $j++) {
  10. $key = html_entity_decode($matches[2][$j]);
  11. $productKey = html_entity_decode($matches[1][$j]);
  12. $categories[$key][$productKey][] = [
  13. "product" => html_entity_decode($matches[1][$j]),
  14. "price" => html_entity_decode($matches[3][$j]),
  15. "currency" => html_entity_decode($matches[4][$j])
  16. ];
  17. }
  18.  
  19. ksort($categories);
  20. $resultArray = [];
  21.  
  22. foreach ($categories as $key => $category) {
  23. ksort($category);
  24. foreach ($category as $cat) {
  25. natcasesort($cat);
  26. foreach ($cat as $k => $v) {
  27. $resultArray[$key][] = $v;
  28. }
  29. }
  30. }
  31.  
  32. echo json_encode($resultArray);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement