PHP - Sort multi-dimensional array by another array

前端 未结 5 1047
忘掉有多难
忘掉有多难 2020-12-06 17:59

I\'m trying to sort a multi-dimensional array by another array, but have so far come up short.
array_multisort seems be working only for real sorting.

Suppose I

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-06 18:51

    There is no built-in function for this in PHP and i am unable to think of any custom function, which would do this using usort. But array_map is simple enough, imo, so why not use it instead?

    $sorted = array_map(function($v) use ($data) {
        return $data[$v - 1];
    }, $order);
    

提交回复
热议问题