Guest User

Untitled

a guest
Feb 19th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. $products = array();
  2.  
  3. $products[] = array('name'=> 'Product 1','description' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
  4. 'location' => 'A city', 'type' => 'Type 1', 'status' => 'new', 'tags'=>'', 'page_url'=>'p1.html', 'image'=>'products/assets/images/1/prod-image.jpg');
  5.  
  6. $products[] = array('name'=> 'Product 2','description' => 'Donec eleifend quam neque, ut mollis massa aliquet id.',
  7. 'location' => 'B city', 'type' => 'Type 1', 'status' => 'under', 'tags'=>'show-homepage', 'page_url'=>'p2.html', 'image'=>'products/assets/images/2/prod-image.jpg');
  8.  
  9. $products[] = array('name'=> 'Product 3','description' => 'Nam non tristique mi.',
  10. 'location' => 'A city', 'type' => 'Type 3', 'status' => 'new', 'tags'=>'', 'page_url'=>'p3.html', 'image'=>'products/assets/images/3/prod-image.jpg');
  11.  
  12. $products[] = array('name'=> 'Product 4','description' => 'Vestibulum accumsan dolor id orci gravida viverra.',
  13. 'location' => 'C city', 'type' => 'Type 2', 'status' => 'new', 'tags'=>'', 'page_url'=>'p4.html', 'image'=>'products/assets/images/4/prod-image.jpg');
  14.  
  15. <form name="search-form" method="GET">
  16.  
  17. <div class="searchform-title">Location</div>
  18. <div style="margin-bottom:20px;">
  19. <select name="location" style="width:100%; padding:5px">
  20. <option value='Any' selected>Any</option>
  21. <option value='A city'>A city</option>
  22. <option value='B City'>B City</option>
  23. <option value='C city'>C city</option>
  24. <option value='D city'>D city</option>
  25. <option value='L city'>L city</option>
  26. <option value='M city'>M city</option>
  27. <option value='T city'>T city</option>
  28. </select>
  29. </div>
  30.  
  31. <div class="searchform-title">Type</div>
  32. <div style="margin-bottom:20px;">
  33. <select name="type" style="width:100%; padding:5px">
  34. <option value='Any' selected>Any</option>
  35. <option value='Type 1'>Type 1</option>
  36. <option value='Type 2'>Type 2</option>
  37. <option value='Type 3'>Type 3</option>
  38. <option value='Type 4'>Type 4</option>
  39. </select>
  40. </div>
  41.  
  42. <div class="searchform-title">Status</div>
  43. <div style="margin-bottom:20px;">
  44. <select name="status" style="width:100%; padding:5px">
  45. <option value='Any' selected>Any</option>
  46. <option value='New'>New</option>
  47. <option value='Old'>Old</option>
  48. <option value='Under'>Under</option>
  49. </select>
  50. </div>
  51.  
  52. <input class="proj-search-btn" type="submit" name="search" value="Search"/>
  53. </form>
  54.  
  55. <?php
  56.  
  57. $products = array();
  58.  
  59. $products[] = array('name'=> 'Product 1','description' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 'location' => 'A city', 'type' => 'Type 1', 'status' => 'new', 'tags'=>'', 'page_url'=>'p1.html', 'image'=>'products/assets/images/1/prod-image.jpg');
  60.  
  61. $products[] = array('name'=> 'Product 2','description' => 'Donec eleifend quam neque, ut mollis massa aliquet id.', 'location' => 'B city', 'type' => 'Type 1', 'status' => 'under', 'tags'=>'show-homepage', 'page_url'=>'p2.html', 'image'=>'products/assets/images/2/prod-image.jpg');
  62.  
  63. $products[] = array('name'=> 'Product 3','description' => 'Nam non tristique mi.', 'location' => 'A city', 'type' => 'Type 3', 'status' => 'new', 'tags'=>'', 'page_url'=>'p3.html', 'image'=>'products/assets/images/3/prod-image.jpg');
  64.  
  65. $products[] = array('name'=> 'Product 4','description' => 'Vestibulum accumsan dolor id orci gravida viverra.', 'location' => 'C city', 'type' => 'Type 2', 'status' => 'new', 'tags'=>'', 'page_url'=>'p4.html', 'image'=>'products/assets/images/4/prod-image.jpg');
  66.  
  67. function search($products, $criteria) {
  68. return array_filter($products, function($product) use ($criteria) {
  69. return array_reduce(array_keys($criteria), function($carry, $key) use ($product, $criteria) {
  70. return $carry && ($product[$key]===$criteria[$key]);
  71. }, true);
  72. });
  73. }
  74.  
  75. $r = search($products, array('location'=>'A city', 'type'=>'Type 3'));
  76. // if you don't care about keys of the array, you can skip sort
  77. sort($r);
  78.  
  79. var_dump($r);
  80.  
  81. array(1) {
  82. [0]=>
  83. array(8) {
  84. ["name"]=>
  85. string(9) "Product 3"
  86. ["description"]=>
  87. string(21) "Nam non tristique mi."
  88. ["location"]=>
  89. string(6) "A city"
  90. ["type"]=>
  91. string(6) "Type 3"
  92. ["status"]=>
  93. string(3) "new"
  94. ["tags"]=>
  95. string(0) ""
  96. ["page_url"]=>
  97. string(7) "p3.html"
  98. ["image"]=>
  99. string(39) "products/assets/images/3/prod-image.jpg"
  100. }
  101. }
Add Comment
Please, Sign In to add comment