代码之家  ›  专栏  ›  技术社区  ›  Mankind1023

如何对查询结果数组排序

  •  0
  • Mankind1023  · 技术社区  · 15 年前

    我有一个数组,其中包含来自常规表的查询结果,例如:

    id | Name | Department | Location | Email | Phone | Type | ..........and so on
    

    我有一个查询数组中的结果,我可以得到默认的查询排序,但我希望能够根据需要使用该数组,而不必不断从服务器重新读取数据,以减少流量/加快速度。

    sort(array by department ascending)
    display array in nice format
    

    有什么想法吗?

    2 回复  |  直到 15 年前
        1
  •  1
  •   Sarfraz    15 年前

    您可以这样对其排序(因为它是关联数组):

    function cmp($a, $b)
    {
       return strcmp($a['department'], $b['department']);
    }
    
    usort($your_array, "cmp")
    print_r($your_array);
    
        2
  •  0
  •   Ankit Jaiswal    15 年前

    您可以使用array\u multisort(),不过您需要根据需要进行更多处理。

    看一看 http://www.phpf1.com/manual/array-multisort.html