简体   繁体   English

按特定值对数组排序

[英]Sort an array by specific values

Here is the scenario, I have a database and I need data that is different from four tables to display in a list. 在这种情况下,我有一个数据库,并且需要与四个表不同的数据才能显示在列表中。 I was having trouble getting the queries to join in a way that would give the results that I needed in the right order and format so I did a bunch of queries and pushed the results to and array. 我很难以能够以正确的顺序和格式给出我需要的结果的方式来加入查询,所以我做了很多查询并将结果推送到and数组。 So now I have this array and it works great so far. 所以现在我有了这个数组,到目前为止效果很好。

To visualize this I have the data listed in table and I need to sort the table based on the heading. 为了对此进行可视化处理,我在表中列出了数据,我需要根据标题对表进行排序。

What I need to do next is some how sort the array by a specific value. 接下来需要做的是一些如何按特定值对数组进行排序。

For example I have the array that contains a value “type” I want to search the array for the specific “types” and then sort the array based on that. 例如,我有一个包含值“ type”的数组,我想在数组中搜索特定的“ types”,然后根据该数组对数组进行排序。 So if the “type” column contains items such as “file” , “link” or “quiz” it would sort alphabetically. 因此,如果“类型”列包含诸如“文件”,“链接”或“测验”之类的项目,则它将按字母顺序排序。

Is this possible? 这可能吗? Or should I be thinking of this in a different way? 还是我应该以不同的方式思考?

Can anyone point me in the right direction? 谁能指出我正确的方向?

In most cases, you'll probably have better results by letting the database do the work, rather than post-processing it in PHP. 在大多数情况下,让数据库完成工作,而不是在PHP中对其进行后处理,可能会获得更好的结果。

As the other folks are saying, some more specifics and code examples would allow us to provide better help, but based on what you've said: 就像其他人所说的那样,一些更多的细节和代码示例将使我们能够提供更好的帮助,但要基于您所说的内容:

To limit your query results to a specific type, you would add a WHERE clause: 要将查询结果限制为特定类型,您可以添加WHERE子句:

SELECT [whatever columns] FROM table_name WHERE type = 'file'

If you want to select multiple types at the same time: 如果要同时选择多种类型:

SELECT [whatever columns] FROM table_name WHERE type IN ('file', 'link', 'quiz')

If you want to order them alphabetically by type: 如果要按类型按字母顺序排序:

SELECT [whatever columns] FROM table_name WHERE type IN ('file', 'link', 'quiz') ORDER BY type ASC

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

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