将 array_multisort() 与自定义函数一起使用


Use array_multisort() with custom function

>我有以下数组,我想根据另一个数组而不是DESCASC对其进行排序

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

是否可以使用 array_multisort() 中的自定义函数执行此操作?

例如:

array_multisort($array['type'], $array['year'], custom_function, $array['note']);

而不是:

array_multisort($array['type'], $array['year'], SORT_ASC, $array['note']);

如果您知道数组的深度,则可以简单地对要排序的每个数组元素应用 usort。

下面是一个根据自定义数组进行排序的示例:

<?php
$order = array(
    'first',
    'second',
    'third',
    'fourth',
    'fifth'
);
$array = array(
    array(
        'second',
        'fourth',
        'first',
        'third'
    ),
    array(
        'second',
        'first'
    )
);
foreach($array as &$value) {
    usort($value, function($a, $b) use($order) {
        return array_search($a, $order) > array_search($b, $order);
    });
}
unset($value);
var_dump($array);
/*
array(2) {
  [0]=>
  array(4) {
    [0]=>
    string(5) "first"
    [1]=>
    string(6) "second"
    [2]=>
    string(5) "third"
    [3]=>
    string(6) "fourth"
  }
  [1]=>
  array(2) {
    [0]=>
    string(5) "first"
    [1]=>
    string(6) "second"
  }
}
*/

如果你不知道数组可以有多深,我想到的唯一解决方案是递归函数:

<?php
$order = array(
    'first',
    'second',
    'third',
    'fourth',
    'fifth'
);
$array = array(
    array(
        'second',
        'fourth',
        'first',
        'third'
    ),
    array(
        array('second', 'first'),
        array('fourth', 'third')
    )
);
function custom_multisort($array, $order) {
    foreach($array as &$value) {
        if(is_array($value[0])) {
            $value = custom_multisort($value, $order);
        } else {
            usort($value, function($a, $b) use($order) {
                return array_search($a, $order) > array_search($b, $order);
            });
        }
    }
    return $array;
}
$array = custom_multisort($array, $order);
var_dump($array);
/*
array(2) {
  [0]=>
  array(4) {
    [0]=>
    string(5) "first"
    [1]=>
    string(6) "second"
    [2]=>
    string(5) "third"
    [3]=>
    string(6) "fourth"
  }
  [1]=>
  array(2) {
    [0]=>
    array(2) {
      [0]=>
      string(5) "first"
      [1]=>
      string(6) "second"
    }
    [1]=>
    array(2) {
      [0]=>
      string(5) "third"
      [1]=>
      string(6) "fourth"
    }
  }
}
*/

为了回答OP的问题,假设数组长度相等,那么以下函数应该可以工作:

function uarray_multisort(array &$array1, callable $callback, mixed &...$rest) {
  uasort($array1, $callback);
  $order = array_keys($array1);
  foreach($rest as $arrayN) {
    foreach ($order as $index) {
      $arrayN[] = $arrayN[$index];
    }
    array_splice($arrayN, 0, count($order));
  }
}

使用uarray_multisort给定 OP 数据的示例,请注意要uarray_multisort的第一个参数采用单个数组,因此需要从输入创建一个临时数组:

$array = array(
  'note' => array('test', 'test1', 'test2', 'test3', 'test4'),
  'year' => array('2011', '2010', '2012', '2009', '2010'),
  'type' => array('journal', 'conference', 'conference', 'conference', 'conference'),
);
function custom_function($a, $b) {
  $cmp = $a['year'] - $b['year'];
  return $cmp !== 0 ? $cmp : strcmp($a['type'], $b['type']);
}
$yearType = array_map(function($year, $type){
  return array('year'=>$year, 'type'=>$type);
}, $array['year'], $array['type']);
uarray_multisort($yearType, 'custom_function', $array['note'], $array['year'], $array['type']);

我认为这是不可能的。而不是像这样

$custom_function_value = custom_function();
array_multisort($array['type'], $array['year'], $custom_function_value, $array['note']);

我认为这将提供您想要的输出。