2012-06-20 109 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