Guest User

Untitled

a guest
Dec 27th, 2012
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.22 KB | None | 0 0
  1. <?php
  2.  
  3. /* { error reporting } */
  4. error_reporting(E_ALL);
  5. ini_set('error_reporting', E_ALL);
  6. ini_set('display_errors', 1);
  7.  
  8. function search($array, $b) {
  9.     $ok = true;
  10.     foreach ($b as $key => $value) {
  11.         if (!isset($array[$key])) { $ok = false; break; }
  12.         if (!is_array($value))
  13.             $ok = ($array[$key] == $value);
  14.         else
  15.             $ok = search($array[$key], $value);
  16.         if ($ok === false) break;
  17.     }
  18.     return $ok;
  19. }
  20.  
  21. $example_array = array(
  22.     'id' => '123',
  23.     'name' => array('first' => 'pete', 'last' => 'foo'),
  24.     'address' => array(
  25.         'shipping' => array('zip' => '1234', 'country' => 'USA')
  26.     )
  27. );
  28.  
  29. // These should return true:
  30. echo (search($example_array, array('id' => '123'))) ? 'true' : 'false'; echo '<br>';
  31. echo (search($example_array, array('name' => array('first' => 'pete')))) ? 'true' : 'false'; echo '<br>';
  32. echo (search($example_array, array('address' => array('shipping' => array('country' => 'USA'))))) ? 'true' : 'false'; echo '<br>';
  33.  
  34. // These don't have to return true:
  35. echo (search($example_array, array('first' => 'pete'))) ? 'true' : 'false'; echo '<br>';
  36. echo (search($example_array, array('country' => 'USA'))) ? 'true' : 'false'; echo '<br>';
  37.  
  38.  
  39. ?>
Advertisement
Add Comment
Please, Sign In to add comment