Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
Array
(
    [0] => Array
        (
            [id] => 7
            [workorder_id] => 27
            [truck_id] => 4
            [event_type] => 1
            [location_id] => 
            [location_name] => Billing Address
            [address_address] => 123 Main Street
            [address_city] => Montreal
            [address_state] => QC
            [address_zip] => A1A1A1
            [address_country_id] => 1
            [contact] => bob
            [phone] => 555-555-555
            [fax] => 555-555-555
            [po] => 123131
            [notes] => 
            [appointment_from] => 2013-03-30 12:30:00
            [appointment_to] => 2013-03-30 14:30:00
            [crossdock] => 0
            [status] => 1
        )

    [1] => Array
        (
            [id] => 8
            [workorder_id] => 27
            [truck_id] => 4
            [event_type] => -1
            [location_id] => 
            [location_name] => Billing Address
            [address_address] => 123 Main Street
            [address_city] => Montreal
            [address_state] => QC
            [address_zip] => A1A1A1
            [address_country_id] => 1
            [contact] => 
            [phone] => 555-555-555
            [fax] => 
            [po] => 
            [notes] => 
            [appointment_from] => 2013-04-04 06:00:00
            [appointment_to] => 2013-04-04 12:00:00
            [crossdock] => 0
            [status] => 1
        )

    [2] => Array
        (
            [id] => 9
            [workorder_id] => 27
            [truck_id] => 4
            [event_type] => 1
            [location_id] => 
            [location_name] => Billing Address
            [address_address] => 123 Main Street
            [address_city] => Montreal
            [address_state] => QC
            [address_zip] => A1A1A1
            [address_country_id] => 2
            [contact] => Jim Smith
            [phone] => 555-555-555
            [fax] => 555-555-555
            [po] => 
            [notes] => 
            [appointment_from] => 2013-04-16 10:00:00
            [appointment_to] => 2013-04-16 12:00:00
            [crossdock] => 0
            [status] => 1
        )

)

Okay, so I have this array, lets call it $array.

I now have a function to sort multidimensional arrays by multiple keys.

function sort_multiple_keys($array,$key1,$key1_sort = SORT_DESC,$key2,$key2_sort = SORT_ASC){

    $sort = array(); 
    if(count($array) > 0){
        foreach($array as $k=>$v) {
            $first[$k] = $v[$key1];
            $second[$k] = $v[$key2];
        }

        array_multisort($first, $key1_sort, $second, $key2_sort, $array);

    }

    unset($sort);
}

So, I would to sort it by the EVENT TYPE and then APPOINTMENT_FROM date. So I run this function:

sort_multiple_keys($array,'event_type',SORT_DESC,'appointment_from',SORT_ASC);

But nothing??

Any help?

share|improve this question
    
Does your function need to return a value? –  andrewsi Apr 11 '13 at 18:03
    
no, it just runs an array sort.. but before I am able to do that, I have to do the conversion. The example I got it from is from here: stackoverflow.com/questions/3232965/… –  Josh Grauer Apr 11 '13 at 18:12

1 Answer 1

up vote 0 down vote accepted

You're not actually returning a value from your sort function.

You can either return $array at the end; or pass it in as a reference, so the function works on the original instance instead of a copy; for that, you need to change the function definition to:

function sort_multiple_keys(&$array,$key1,$key1_sort = SORT_DESC,$key2,$key2_sort = SORT_ASC){
                            ^ tells PHP that this variable is by reference
share|improve this answer
    
that worked.. perfect.. thanks a lot. –  Josh Grauer Apr 11 '13 at 18:14
    
Glad to have helped. –  andrewsi Apr 11 '13 at 18:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.