简体   繁体   English

如何对查询结果数组进行排序

[英]How to sort a query result array

I have a array with results from a query from regular tables, for example: 我有一个数组,其中包含来自常规表的查询结果,例如:

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

I have the results in a query array, I can get the default query sorted, but I would like to ability to be able to resort that array as needed without having to keep re-reading data from the server, to cut down on traffic / speed things up. 我将结果存储在查询数组中,可以对默认查询进行排序,但是我希望能够根据需要使用该数组,而不必继续从服务器读取数据,从而减少了流量/加快速度。

Is there a native function that does that so I can go like: 有没有这样做的本机功能,所以我可以这样:

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

Any ideas? 有任何想法吗?

You can sort it this way (since it is an associative array): 您可以通过以下方式对其进行排序(因为它是一个关联数组):

function cmp($a, $b)
{
   return strcmp($a['department'], $b['department']);
}

usort($your_array, "cmp")
print_r($your_array);

You can use array_multisort(), though you need some more processing as per your requirements. 您可以使用array_multisort(),尽管根据需要还需要更多处理。

Have a look at it http://www.phpf1.com/manual/array-multisort.html 看看它http://www.phpf1.com/manual/array-multisort.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM