【问题标题】:Sort array according to the search string occurence php根据搜索字符串出现php对数组进行排序
【发布时间】:2012-11-04 21:28:47
【问题描述】:

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

)

请帮帮我!!!!

【问题讨论】:

    标签: php mysql multidimensional-array sorting


    【解决方案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 查询中对结果进行排序,这样会提高性能。

    【讨论】:

      猜你喜欢
      • 2016-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-05
      • 1970-01-01
      相关资源
      最近更新 更多