-1

I have 2 array of which one is for order and another where the real value has to be sorted. How can I sort the array by comparing array? Any Idea will be really helpful. Thank You.

Order Array:

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
    )
)

And To Sort Array:

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

1 Answers1

0

You could use uksort in combination with array_combine to create a key-order mapping from the order array.

// 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];
});
Philipp
  • 15,377
  • 4
  • 35
  • 52