Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* { error reporting } */
- error_reporting(E_ALL);
- ini_set('error_reporting', E_ALL);
- ini_set('display_errors', 1);
- function search($array, $b) {
- $ok = true;
- foreach ($b as $key => $value) {
- if (!isset($array[$key])) { $ok = false; break; }
- if (!is_array($value))
- $ok = ($array[$key] == $value);
- else
- $ok = search($array[$key], $value);
- if ($ok === false) break;
- }
- return $ok;
- }
- $example_array = array(
- 'id' => '123',
- 'name' => array('first' => 'pete', 'last' => 'foo'),
- 'address' => array(
- 'shipping' => array('zip' => '1234', 'country' => 'USA')
- )
- );
- // These should return true:
- echo (search($example_array, array('id' => '123'))) ? 'true' : 'false'; echo '<br>';
- echo (search($example_array, array('name' => array('first' => 'pete')))) ? 'true' : 'false'; echo '<br>';
- echo (search($example_array, array('address' => array('shipping' => array('country' => 'USA'))))) ? 'true' : 'false'; echo '<br>';
- // These don't have to return true:
- echo (search($example_array, array('first' => 'pete'))) ? 'true' : 'false'; echo '<br>';
- echo (search($example_array, array('country' => 'USA'))) ? 'true' : 'false'; echo '<br>';
- ?>
Advertisement
Add Comment
Please, Sign In to add comment