2013-04-11 69 views
0
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 
     ) 

) 

好吧,所以我有这个数组,让我们叫它$ array。php array_multisort不排序我的多维数组

我现在有一个函数可以用多个键对多维数组进行排序。

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); 
} 

所以,我会按事件类型,然后APPOINTMENT_FROM日期进行排序。所以我运行这个功能:

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

但没有什么?

任何帮助?

+0

您的函数是否需要返回值? – andrewsi 2013-04-11 18:03:33

+0

不,它只是运行一个数组排序..但在我能够做到这一点之前,我必须进行转换。我从它得到的例子是从这里: http://stackoverflow.com/questions/3232965/sort-multidimensional-array-by-multiple-keys – 2013-04-11 18:12:16

回答

0

你实际上并没有从你的排序函数中返回一个值。

您可以在末尾return $array;或者将其作为参考传入,因此该函数可以在原始实例上工作,而不是在副本上工作;为此,您需要将函数定义更改为:

function sort_multiple_keys(&$array,$key1,$key1_sort = SORT_DESC,$key2,$key2_sort = SORT_ASC){ 
          ^tells PHP that this variable is by reference 
+0

that worked .. perfect ..非常感谢。 – 2013-04-11 18:14:52

+0

很高兴有帮助。 – andrewsi 2013-04-11 18:16:22