PHP数组按照指定顺序排序

in 前端 with 0 comment
$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];

$rule = [3, 5, 4, 2, 1, 6, 9, 7, 8];

usort($arr, function ($a, $b) use ($rule) {
    return (array_search($a, $rule) < array_search($b, $rule)) ? -1 : 1;
});


var_dump($arr);
Comments are closed.