PHP 5.2记录集数组的GENERIC排序所需的函数 [英] PHP 5.2 Function needed for GENERIC sorting of a recordset array

查看:77
本文介绍了PHP 5.2记录集数组的GENERIC排序所需的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在有人必须想出一个解决方案。我们使用PHP 5.2。 (不要问我为什么。)我写了一个PHP类来显示一个记录集作为一个HTML表/ datagrid,我希望扩展它,以便我们可以通过用户选择的任何列来排序datagrid。在下面的示例数据中,我们可能需要通过Name,Shirt,Assign或Age字段对记录集数组进行排序。我将照顾显示部分,我只需要帮助排序数据数组。

Somebody must have come up with a solution for this by now. We are using PHP 5.2. (Don't ask me why.) I wrote a PHP class to display a recordset as an HTML table/datagrid, and I wish to expand it so that we can sort the datagrid by whichever column the user selects. In the below example data, we may need to sort the recordset array by Name, Shirt, Assign, or Age fields. I will take care of the display part, I just need help with sorting the data array.

像往常一样,我查询数据库以获取结果,迭代结果,并将记录放入一个assciateiave数组。所以,我们最终得到一个数组数组。 (见下文)我需要能够通过数据集中的任何列进行排序。但是,我在设计时不知道列名,也不知道列是字符串还是数字值。

As usual, I query a database to get a result, iterate throught he result, and put the records into an assciateiave array. So, we end up with an array of arrays. (See below.) I need to be able to sort by any column in the dataset. However, I will not know the column names at design time, nor will I know if the colums will be string or numeric values.

我看到了很多解决方案这,但我还没有看到一个GOOD和GENERIC解决方案有人可以建议一种方式,我可以排序的记录集数组是GENERIC,并将工作在任何记录集?再次,我不会知道字段名称或数据类型在设计时。下面给出的数组只是一个例子。

I have seen a ton of solutions to this, but I have not seen a GOOD and GENERIC solution Can somebody please suggest a way that I can sort the recordset array that is GENERIC, and will work on any recordset? Again, I will not know the fields names or datatypes at design time. The array presented below is ONLY an example.

更新:是的,我想让数据库做排序,但这不会发生。我们运行的查询非常复杂。 (我不是真正查询星际迷航字符的表。)它们包括连接,限制和复杂的WHERE子句。编写一个函数来挑选SQL语句以添加一个ORDER BY真的不是一个选项。此外,有时我们已经有了查询结果的数组,而不是运行新查询的能力。

UPDATE: Yes, I would love to have the database do the sorting, but that is just not going to happen. The queries that we are running are very complex. (I am not really querying a table of Star Trek characters.) They include joins, limits, and complex WHERE clauses. Writing a function to pick apart the SQL statement to add an ORDER BY is really not an option. Besides, sometimes we already have the array that is a result of the query, rather than the ability to run a new query.

Array
(
    [0] => Array
        (
            [name] => Kirk
            [shrit] => Gold
            [assign] => Bridge
        )

    [1] => Array
        (
            [name] => Spock
            [shrit] => Blue
            [assign] => Bridge
        )

    [2] => Array
        (
            [name] => Uhura
            [shrit] => Red
            [assign] => Bridge
        )

    [3] => Array
        (
            [name] => Scotty
            [shrit] => Red
            [assign] => Engineering
        )

    [4] => Array
        (
            [name] => McCoy
            [shrit] => Blue
            [assign] => Sick Bay
        )

)


推荐答案

在数据库中(它更高效)。如果不是

Well, first off, you should be sorting in the database (it's more efficient). If not

在php 5.3 +:

In php 5.3+:

function getSortCommand($field, $sortfunc) {
    return function($var1, $var2) use ($field, $sortfunc) {
        return $sortfunc($var1[$field], $var2[$field]);
    }
}

在php< = 5.2创建一个纯函数:

in php <= 5.2, you'll need to create a pure function:

function getSortCommand($field, $sortfunc) {
    return create_function('$var1, $var2', 'return '.$sortfunc.'($var1["'.$field.'"], $var2["'.$field.'"]);');
}

两者的用法:

$array = usort($array, getSortCommand('name', 'strnatcasecmp'));

要调整字段,只需更改$ field参数。要更改排序函数,只需更改$ sortfunc参数...

To adjust the field, you just change the $field parameter. To change the sort function, just change the $sortfunc parameter...

这篇关于PHP 5.2记录集数组的GENERIC排序所需的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆