PHP and Web Technologies Freak
I’m working on a security and trying to make searches in a multi-dimensional array
Here is the function;
<?php function multi_array_search($needle, $haystack, $key, &$result, $searchlevel = 0) { while(is_array($haystack) && isset($haystack[key($haystack)])) { if($searchlevel == 0 && key($haystack) == $key && $haystack[$key] == $needle) { $result = $haystack; } elseif($searchlevel > 0) { multi_array_search($needle, $haystack[key($haystack)], $key, $result, $searchlevel - 1); } next($haystack); } } ?>
Example;
2-dimensional array, search by both key and value
$arr1 = array(
1 => array(‘id’ => 1, ‘name’ => ‘Ersin’, ‘gender’ => ‘male’),
…
12 => array(‘id’ => 12, ‘name’ => ‘Emel’, ‘gender’ => ‘female’),
);
<?php array_search_in_level('Emel', $arr1, 'name', $result, 1); ?>
$result is:
array(
‘id’ => int 12
‘name’ => string ‘Emel’ (length=4)
‘gender’ => string ‘female’ (length=4)
)
Copyright © 2007 - Ersin Acar - is proudly powered by WordPress
Dilectio Theme is created by: Design Disease brought to you by Smashingmagazine.com
Leave a reply