我有以下数组:
$array = [
'note' => [],
'year' => ['2011','2010', '2012'],
'type' => ['conference', 'journal', 'conference'],
];
我使用以下函数根据类型字段和另一个数组来排序数组:
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;
}
当我调用以下函数时,出现了以下错误:
$array = array_multisort_by_order($array, 'type', array('conference', 'journal'));
print_r($array['type']);
错误:
Warning: array_multisort(): Array sizes are inconsistent.
我知道数组是不一致的。是否有更好的函数可用?
请检查:codepad 期望输出:
Array
(
[note] => Array
(
[0] =>
[1] =>
[2] =>
)
[year] => Array
(
[0] => 2011
[1] => 2012
[2] => 2010
)
[type] => Array
(
[0] => conference
[1] => conference
[2] => journal
)
)
示例2:
数组
$array = [
'note' => ['test1', 'test2'],
'year' => ['2011', '2012'],
'type' => ['conference', 'journal', 'conference'],
];
期望结果2
Array
(
[note] => Array
(
[0] => test1
[1] =>
[2] => tes2
)
[year] => Array
(
[0] => 2011
[1] => 2012
[2] =>
)
[type] => Array
(
[0] => conference
[1] => conference
[2] => journal
)
)