0

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 this 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?

Community
  • 1
  • 1
Daniel
  • 147
  • 1
  • 2
  • 9

1 Answers1

0

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.

xdazz
  • 158,678
  • 38
  • 247
  • 274
  • It's looks very difficult, Could you help me with that function? – Daniel Aug 28 '11 at 15:40
  • @Daniel Edited. Please check. – xdazz Aug 28 '11 at 15:55
  • I unsuccess to understand how is going to work with my code that i posted at the first post. :S – Daniel Aug 28 '11 at 15:59
  • It resorts your $test array. What is the problem? – xdazz Aug 28 '11 at 16:03
  • I want to sort by: $arr = array("hello", "try", "hel", "hey hello"); $search = "hey"; //your search var for($i=0; $i $v) { $sorted_arr[] = $arr[$k]; } – Daniel Aug 28 '11 at 16:05
  • @Daniel You means you want to use `levenshtein` to cmpare? So what you need to do is replace the cmp function `strcmp` with your own compare logic. – xdazz Aug 28 '11 at 16:11
  • What does `levenshtein` returns ? int? – xdazz Aug 28 '11 at 16:25
  • "This function returns the Levenshtein-Distance between the two argument strings or -1, if one of the argument strings is longer than the limit of 255 characters. " I don't really know what it means. – Daniel Aug 28 '11 at 16:29
  • So if you don't know what it means, why do you want to use it in your code . – xdazz Aug 28 '11 at 16:34
  • I know that it sort my array like I want to :) its enough for me ^^ – Daniel Aug 28 '11 at 16:46