fork download
  1. <?php
  2.  
  3. $products = array();
  4.  
  5. $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');
  6.  
  7. $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');
  8.  
  9. $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');
  10.  
  11. $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');
  12.  
  13.  
  14. function search($array, $key, $value)
  15. {
  16. $results = array();
  17.  
  18. if (is_array($array)) {
  19. if (isset($array[$key]) && $array[$key] == $value) {
  20. $results[] = $array;
  21. }
  22.  
  23. foreach ($array as $subarray) {
  24. $results = array_merge($results, search($subarray, $key, $value));
  25. }
  26. }
  27.  
  28. return $results;
  29. }
  30.  
  31.  
  32.  
  33. $the_products = search($products, 'location', 'A city');
  34.  
  35. foreach($the_products as $product){
  36.  
  37. echo $product['image']; echo "<br/>";
  38. echo ucwords($product['name']); echo "<br/>";
  39. echo ucwords($product['location']); echo "<br/>";
  40. echo substr($product['description'],0, 230); echo "<br/>";
  41. echo $product['page_url']; echo "<br/>";
  42.  
  43. }
Success #stdin #stdout 0.01s 82880KB
stdin
Standard input is empty
stdout
products/assets/images/1/prod-image.jpg<br/>Product 1<br/>A City<br/>Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br/>p1.html<br/>products/assets/images/3/prod-image.jpg<br/>Product 3<br/>A City<br/>Nam non tristique mi.<br/>p3.html<br/>