排序一个数组,比较PHP中的另一个数组

时间:2018-09-04 10:33:42

标签: php arrays

我有2个数组,其中一个用于订购,另一个用于对实际值进行排序。如何通过比较数组对数组进行排序?任何想法都会非常有帮助。谢谢。

订单数组:

Array(
[0] => Array
    (
        [code] => subscription_bundles
        [id_sequency] => 1
    )

[1] => Array
    (
        [code] => pre_order
        [id_sequency] => 2
    )

[2] => Array
    (
        [code] => voucher
        [id_sequency] => 3
    )

[3] => Array
    (
        [code] => ppv
        [id_sequency] => 4
    )

[4] => Array
    (
        [code] => ppv_bundle
        [id_sequency] => 5
    )
)

对数组排序:

Array(
[monetization] => Array
    (
        [ppv] => Pay Per View
        [ppv_bundle] => Pay-Per-View Bundle
        [subscription_bundles] => Subscription
        [voucher] => Voucher
    )

1 个答案:

答案 0 :(得分:0)

您可以结合使用uksortarray_combine来从订单数组中创建键订单映射。

// create order mapping
$map = array_combine(
    array_column($order, 'code'),
    array_column($order, 'id_sequency')
);

// sort the data
uksort($data, function ($a, $b) use ($map) {
    return $map[$a] - $map[$b];
});