okay, so lemme see if I remember how to ask a question 🙂

I have this multi-dimensional array that I need to dynamically sort.
I need to be able to specify to sort by the key of the sub-array
ie.. "1" to sort by Part_number, "4" to sort by Reason, and so on
I also need to go ascending or descending with it. So I looked through
the manual, and saw like 10 different array sort functions, and am kind
of stuck on which one I should use. Hoping someone can nudge me in the right
direction. Was thinking usort(). This the best one for this? Below's a
look at what my array will look like. Thanks 😃

$results =
Array
(
    [0] => Array
        (
            [0] => License_Plate
            [1] => Part_Number
            [2] => Transaction_Date
            [3] => Performed_By
            [4] => Reason
        )

[1] => Array
    (
        [0] => License_Plate
        [1] => Part_Number
        [2] => Transaction_Date
        [3] => Performed_By
        [4] => Reason
    )
)

    //Ordering Function. This allows me to sort the array by score.
    function Compare($ar1, $ar2)
    {
    if ($ar1['score']>$ar2['score'])
    return -1;
    else if ($ar1['score']<$ar2['score'])
    return 1;
    / IF i want to order by Company ID
    if ($ar1['id']>$ar2['id'])
    return -1;
    else if ($ar1['id']<$ar2['id'])
    return 1;
    /
    return 0;
    }

    //Just and example of the array i built
    $id[]=array(score=>$TSCORE,id=>$TID);

    //Order the array so the highest score comes first.Compare is a funciton see top
    uasort($id, 'Compare');

    just fiddle with that function to get your desired result

      I saw something like that on php.net. But I need to pass a sort to it, to do it dynamically. Can't hard code the field to sort in it. The way around that was to create this function five times, one for each field. Seems redundant. Any ideas?

        just hack the function. It is really quite a simple function. Add some new properties to it so that it will accept dynamic values. a coupld of global variables inside the fucntion that refer to variables outside it will probably work.

        I think something like 'global $temp' is all yah need. But without some actual figures and details of what you are doing it is quite hard to suggest solutions for you 🙂

          Write a Reply...