开发者

Array advanced search by two parameters

开发者 https://www.devze.com 2023-03-31 20:28 出处:网络
I saw this topic And I have used this code: $arr = array(\"hello\", \"try\", \"hel\", \"hey hello\"); $search = \"hey\"; //your search var

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消