Array advanced search by two parameters
I saw this topic And I have used this code:
$arr = array("hello", "try", "hel", "hey hello");
$search = "hey"; //your search var
for ($i=0; $i<count($arr); $i++) {
$temp_arr[$i] = levenshtein($search, $arr[$i]);
}
asort($temp_arr);
foreach ($temp_arr as $k => $v) {
$sorted_arr[] = $arr[$k];
}
But now I want to search by array in array. For example, if I have thi开发者_如何学运维s array:
$test = array(
0 => array("google", "http://www.google.com"),
1 => array("test", "http://www.test.com"),
2 => array("test2", "http://www.test2.com")
)
So I want resort by $test[$x][0]
( $x = num
), How can I do that?
You can use uasort. Example:
uasort($test, function($a, $b){
return strcmp($a[0], $b[0]);
});
If you php version < 5.3, you need to define the cmp function.
精彩评论