0

我想根据 php 中搜索字符串的出现对二维数组进行排序。我试图根据搜索字符串的相关性来获取数据。我遇到了比赛和反对,但它可以在MYISAM上工作,但我的桌子在innodb中。所以计划使用任何排序函数对数组进行排序,但我什么也找不到。

我的搜索压力测试字符串数组

  Array
   (
      [0] => pressure
       [1] => tes
    )

我的输出数组匹配上面的字符串和标题是

 Array
(
[0] => Array
    (
        [title] => tests.doc
        [link] => http://localhost/test.doc
        [snippet] => 
    )

[1] => Array
    (
        [title] => Pressure Testing Your Company
        [link] => http://localhost/Pressure_Testing_Your_Companys.pdf
        [snippet] => Questions used by the CFO against dimensions critical to success
    )

[2] => Array
    (
        [title] => pressure.doc
        [link] => http://localhost/pressure.doc
        [snippet] => Templates for services
    )

)

在上面的数组中,最相关的 array[1] 然后是 array[2] 然后是 array[0] 应该是这个顺序。我想相应地对这个数组进行排序。我的输出应该如下所示

 Array
(

[0] => Array
    (
        [title] => Pressure Testing Your Company
        [link] => http://localhost/Pressure_Testing_Your_Companys.pdf
        [snippet] => Questions used by the CFO against dimensions critical to success
    )

[1] => Array
    (
        [title] => pressure.doc
        [link] => http://localhost/pressure.doc
        [snippet] => Templates for services
    )
[2] => Array
    (
        [title] => tests.doc
        [link] => http://localhost/test.doc
        [snippet] => 
    )

)

请帮我!!!!

4

1 回答 1

1

看看例子#3,我想这就是你想要的。

http://php.net/manual/en/function.array-multisort.php

刚刚测试了这个,它似乎正在工作:

$titles = array();

foreach ( $array as $key => $value ) 
{
    $titles[ $key ] = $value['title'];
} 

array_multisort( $titles, SORT_ASC , $array );

我强烈建议您在 MySQL 查询中对结果进行排序,这样性能会好得多。

于 2012-11-16T07:17:25.930 回答