PHP Function needed for GENERIC sorting of a recordset array

Posted by donbriggs on Stack Overflow See other posts from Stack Overflow or by donbriggs
Published on 2010-05-21T17:48:51Z Indexed on 2010/05/21 17:50 UTC
Read the original article Hit count: 239

Filed under:
|
|
|

Somebody must have come up with a solution for this by now. 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.

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.

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.

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
    )

)

© Stack Overflow or respective owner

Related posts about php

Related posts about database