2012-06-20 105 views
0

考慮以下數組:排序PHP數組由其他數組

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

陣列可如果是比較容易使用下面的函數很容易地轉化:

for($i=0; $i < count($array['type']); $i++) 
    foreach($array as $key=> $value) 
    $temp[$i][$key] = $value[$i]; 

print_r($temp); 

我想對它進行排序使用以下標準:

  1. 陣列​​
  2. 在對類別進行排序後,我想按每個類別排序year DESC

期望的結果:

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

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

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

) 
+0

這不是分組相關內容的一個好辦法。 –

+0

這似乎是你通過將這些東西存儲在一個二維數組中而使它變得不必要的困難。 – Daniel

+0

那麼這是不可能做到這一點?因爲我發現每個使用PHP排序函數的解決方案(它們都使用快速排序),因此返回正確結果是不可能的。 – glarkou

回答

2
<?php 
$Array= array(array('note'=>'test1','year'=>'2011','type'=>'journal'), 
       array('note'=>'test2','year'=>'2012','type'=>'editorial'), 
       array('note'=>'test3','year'=>'2012','type'=>'conference'), 
       array('note'=>'test4','year'=>'2012','type'=>'conference'), 
       array('note'=>'test5','year'=>'2012','type'=>'conference')); 
print_r($Array); 
/*Array 
(
    [0] => Array 
     (
      [note] => test1 
      [year] => 2011 
      [type] => journal 
     ) 

    [1] => Array 
     (
      [note] => test2 
      [year] => 2012 
      [type] => editorial 
     ) 

    [2] => Array 
     (
      [note] => test3 
      [year] => 2012 
      [type] => conference 
     ) 

    [3] => Array 
     (
      [note] => test4 
      [year] => 2012 
      [type] => conference 
     ) 

    [4] => Array 
     (
      [note] => test5 
      [year] => 2012 
      [type] => conference 
     ) 

)*/ 

//Lets sort them by value 
function array_sort_by_column(&$arr, $col, $dir = SORT_DESC) { 
    $sort_col = array(); 
    foreach ($arr as $key=> $row) { 
     $sort_col[$key] = $row[$col]; 
    } 
    return array_multisort($sort_col, $dir, $arr); 
} 

array_sort_by_column($Array, 'year'); 
print_r($Array); 
/*Array 
(
    [0] => Array 
     (
      [note] => test2 
      [year] => 2012 
      [type] => editorial 
     ) 

    [1] => Array 
     (
      [note] => test3 
      [year] => 2012 
      [type] => conference 
     ) 

    [2] => Array 
     (
      [note] => test4 
      [year] => 2012 
      [type] => conference 
     ) 

    [3] => Array 
     (
      [note] => test5 
      [year] => 2012 
      [type] => conference 
     ) 

    [4] => Array 
     (
      [note] => test1 
      [year] => 2011 
      [type] => journal 
     ) 

)*/ 


array_sort_by_column($Array, 'note'); 
print_r($Array); 
/* 
Array 
(
    [0] => Array 
     (
      [note] => test5 
      [year] => 2012 
      [type] => conference 
     ) 

    [1] => Array 
     (
      [note] => test4 
      [year] => 2012 
      [type] => conference 
     ) 

    [2] => Array 
     (
      [note] => test3 
      [year] => 2012 
      [type] => conference 
     ) 

    [3] => Array 
     (
      [note] => test2 
      [year] => 2012 
      [type] => editorial 
     ) 

    [4] => Array 
     (
      [note] => test1 
      [year] => 2011 
      [type] => journal 
     ) 

)*/ 

array_sort_by_column($Array, 'type'); 
print_r($Array); 
/* 
Array 
(
    [0] => Array 
     (
      [note] => test1 
      [year] => 2011 
      [type] => journal 
     ) 

    [1] => Array 
     (
      [note] => test2 
      [year] => 2012 
      [type] => editorial 
     ) 

    [2] => Array 
     (
      [note] => test3 
      [year] => 2012 
      [type] => conference 
     ) 

    [3] => Array 
     (
      [note] => test4 
      [year] => 2012 
      [type] => conference 
     ) 

    [4] => Array 
     (
      [note] => test5 
      [year] => 2012 
      [type] => conference 
     ) 

) 

*/ 
?> 
+0

結果似乎是正確的,但我認爲如果sortby數組不同,那麼結果就會出錯。請再次檢查第一條標準。 – glarkou