使用另一个数组作为输入排序数组

时间:2012-06-13 19:16:24

标签: php arrays multidimensional-array

我想使用下面提供的另一个数组对以下数组进行排序。

例如。我有以下数组:

[note] => Array
(
   [0] => 'note1'
   [1] => 'note2'
   [2] => 'note3'
), 
[text] => Array
(
   [0] => 'text1'
   [2] => 'test3'
), 
[year] => Array
(
   [0] => '2002'
   [1] => '2000'
   [2] => '2011'
)

我有阵列:

$sortBy = array('2011', '2000', '2002').

我想根据$ sortBy数组的值对上面的数组进行排序。

期望的结果是:

[note] => Array
(
   [0] => 'note3'
   [1] => 'note2'
   [2] => 'note1'
), 
[text] => Array
(
   [0] => 'test3'
   [2] => 'text1'
), 
[year] => Array
( 
   [0] => '2011'
   [1] => '2000'
   [2] => '2002'
)

4 个答案:

答案 0 :(得分:3)

你有一个数组:

$array = array(
    'note' => array('note1', 'note2', 'note3'),
    'text' => array('text1', 'text2', 'text3'),
    'year' => array('2002', '2000', '2011')
);

以及某事的顺序:

$sortBy = array('2011', '2000', '2002');
$sortOrder = $array['year'];

然后您可以了解如何对年份进行排序:

array_walk($sortOrder, function(&$v) use ($sortBy) {$v = array_search($v, $sortBy);});

然后根据该顺序对整个数组进行排序:

array_multisort($sortOrder, $array['note'], $array['text'], $array['year']);

完整示例:

$array = array(
    'note' => array('note1', 'note2', 'note3'),
    'text' => array('text1', 'text2', 'text3'),
    'year' => array('2002', '2000', '2011')
);
$sortBy = array('2011', '2000', '2002');
$sortOrder = $array['year'];
array_walk($sortOrder, function(&$v) use ($sortBy) {$v = array_search($v, $sortBy);});
array_multisort($sortOrder, $array['note'], $array['text'], $array['year']);

输出/ Demo

Array(
    [note] => Array(
            [0] => note3
            [1] => note2
            [2] => note1
        )
    [text] => Array(
            [0] => text3
            [1] => text2
            [2] => text1
        )
    [year] => Array(
            [0] => 2011
            [1] => 2000
            [2] => 2002
        )
)

编辑:映射的变体可以省略array_search

$sortBy = array_flip(array('2011', '2000', '2002'));
$sortOrder = $array['year'];
array_walk($sortOrder, function(&$v) use ($sortBy) {$v = $sortBy[$v];});
array_multisort($sortOrder, $array['note'], $array['text'], $array['year']);

Edit2: PHP 5.2包装成单个函数,完全参数化:

/**
 * @param array $array
 * @param string|int $by key/offset
 * @param array $order
 * @return array
 */
function array_multisort_by_order(array $array, $by, array $order)
{
    $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);
    return $array;
}

// Usage:
array_multisort_by_order($array, 'year', array('2011', '2000', '2002'));

答案 1 :(得分:2)

我会像这样制作一个函数:

function sort_by($arr, $sub, $order){
    // Create a map from old key to new key
    $value_kmap = array_flip($arr[$sub]);
    $sort_kmap = array_flip($order);
    foreach($order as $value)
        $kmap[$value_kmap[$value]] = $sort_kmap[$value];

    // Create your result array
    foreach($arr as $name => $sub_arr)
        foreach($kmap as $key => $new_key)
            if(isset($sub_arr[$key]))
                $result[$name][$new_key] = $sub_arr[$key];

    return $result;
}

该功能是通用的,不提及year。它接受子数组的名称作为参数排序。你可以这样使用它:

$arr = array(
    'note' => array('note1', 'note2', 'note3'),
    'text' => array('text1', 'text3'),
    'year' => array('2002',  '2000',  '2011' )
);

print_r(sort_by($arr, 'year', array('2011', '2000', '2002')));

输出[Codepad]:

Array
(
    [note] => Array
        (
            [0] => note3
            [1] => note2
            [2] => note1
        )

    [text] => Array
        (
            [1] => text3
            [2] => text1
        )

    [year] => Array
        (
            [0] => 2011
            [1] => 2000
            [2] => 2002
        )

)

答案 2 :(得分:0)

我将您的数组命名为$ haystack。

$newArray = array(); // not necessary, but starts your new array
// you know how you want to sort it, so loop through your sorting array
foreach( $sortBy as $year ){ 
   // get the key of the 2nd level of the 2D array, which we are saving as $k
   foreach( $haystack['year'] as $key => $value ){
      if( $value == $year ) $k = $key;
      break; // since we got what we need, we don't need to let this foreach loop finish
      // you could also do an array_search() here instead of this foreach
   }
   if( $k > 0 ){
      // if k is 0, we didn't find that value so we're not going to sort it or include it in the new array
      foreach( $haystack as $key => $a ){ 
         // just save it into a new array
         $newArray[$key][$k] = $a;       
      }
   }
}
var_dump( $newArray );

答案 3 :(得分:0)

未经测试(当您发布这些问题时,您应该提供数组的var_export())

//$orig = ...

$sorted = array();

$map = array_flip($orig['year']);
foreach ($sortBy as $year) {
    $originalNumerickey = $map[$year];
    foreach ($orig as $stringKey => $subArray) {
        $sorted[$stringKey][$originalNumerickey] = $subArray[$originalNumerickey];
    }
}