2012-06-20 20 views
0

我有以下的PHP數組,我想按類型先按年份,然後對它進行排序:arra_multisort使用參數的同時

$array = array(
     'note' => array('test', 'test1', 'test2', 'test3', 'test4'), 
     'year' => array('2011','2010', '2012', '2009', '2010'), 
     'type' => array('conference', 'journal', 'conference', 'conference','conference'), 
    ); 

直到現在我所做的:(codepad)是通過排序鍵入,但不是按年份。

<?php 
/** 
* @param array $array 
* @param string|int $by key/offset 
* @param array $order 
* @return array 
*/ 
function array_multisort_by_order(array $array, $by, array $order) 
{ 
    $max = max(array_map('count',$array)); 
    //or, alternatively, depending on input (if there are no 'complete' subarrays): 
    //$max = max(array_map(function($arr){return max(array_keys($arr));},$array))+1; 
    foreach($array as &$sub){ 
     $addin = array_diff_key(array_fill(0,$max,null),$sub); 
     $sub = $addin + $sub; 
     ksort($sub); 
    } 
    $order = array_flip($order); 
    $params[] = $array[$by]; 
    foreach($params[0] as &$v) $v = $order[$v]; 
    foreach($array as &$v) $params[] = &$v; unset($v); 
    call_user_func_array('array_multisort', $params); 
    //no closeures here: 
    //foreach($array as &$sub) $sub = array_filter(function($a){return !is_null($a);},$sub); 
    $filter = create_function('$a','return !is_null($a);'); 
    foreach($array as &$sub) $sub = array_filter($sub,$filter); 
    return $array; 
} 
$array = array(
     'note' => array('test', 'test1', 'test2', 'test3', 'test4'), 
     'year' => array('2011','2010', '2012', '2009', '2010'), 
     'type' => array('conference', 'journal', 'conference', 'conference','conference'), 
    ); 

print_r($array); 

// Usage: 
$array = array_multisort_by_order($array, 'type', array('conference', 'journal')); 

print_r($array); 
?> 

結果是:

Array 
(
[note] => Array 
    (
     [0] => test 
     [1] => test2 
     [2] => test3 
     [3] => test4 
     [4] => test1 
    ) 

[year] => Array 
    (
     [0] => 2011 
     [1] => 2012 
     [2] => 2009 
     [3] => 2010 
     [4] => 2010 
    ) 

[type] => Array 
    (
     [0] => conference 
     [1] => conference 
     [2] => conference 
     [3] => conference 
     [4] => journal 
    ) 

) 

所需的輸出:(按類型和年份排序。)

Array 
(
[note] => Array 
    (
     [0] => test2 
     [1] => test 
     [2] => test4 
     [3] => test3 
     [4] => test1 
    ) 

[year] => Array 
    (
     [0] => 2012 
     [1] => 2011 
     [2] => 2010 
     [3] => 2009 
     [4] => 2010 
    ) 

[type] => Array 
    (
     [0] => conference 
     [1] => conference 
     [2] => conference 
     [3] => conference 
     [4] => journal 
    ) 

) 

回答

0

您可以使用下面的代碼進行排序,你所提到的方法。

<? 

$array = array(
     'note' => array('test', 'test1', 'test2', 'test3', 'test4'), 
     'year' => array('2011','2010', '2012', '2009', '2010'), 
     'type' => array('conference', 'journal', 'conference', 'conference','conference'), 
); 


// Storing the original keys 
$original_keys = array_keys($array); 

$temp_array = array(); 

// Restructuring the array for sorting 
foreach($array as $items){ 
    $index = 0; 
     foreach($items as $item){ 
      $temp_array[$index++][] = $item; 
     } 
} 


array_multisort($array['type'], SORT_ASC, SORT_STRING, $array['year'], SORT_DESC, $temp_array); 

// Restoring the temp array in original format 
$array = array(); 
foreach($temp_array as $items){ 
    $index = 0; 
    foreach($original_keys as $key){ 
     $array[$key][] = $items[$index++]; 
    } 
} 
print_r($array); 

在工作演示: http://codepad.org/GMpf7pFt

+0

感謝YOUT時間。問題是我也想根據另一個數組的值來使用'$ array ['type']'。例如,在數組中,我通過'$ array = array_multisort_by_order($ array,'type',array('conference','journal'));' – glarkou

+0

'對數組進行排序對不起,但我不明白你想要實現什麼這裏。我只是看着輸入和預期的輸出,並提出了一個上面的代碼。 – sushil

+0

是伴侶。謝謝你的時間。 – glarkou