根据搜索字符串出现php对数组进行排序

时间:2012-11-16 06:49:24

标签: php mysql multidimensional-array sorting

我想根据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
    )

在上面的数组中,最相关的数组[1]然后是数组[2],然后数组[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] => 
    )

)

请帮帮我!!!!

1 个答案:

答案 0 :(得分: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查询中对结果进行排序,这样性能会更好。