使用具有排序键的另一个数组对数组进行排序

How to sort an 1st array with 2nd array which has sorted keys in 2nd array without using any loop.

1st Array.

$chunk = array(
                [0] => Array
                    (
                        [id] => 212
                        [order] => 1
                        [title] => fdfdfdfdf
                    )

                [1] => Array
                    (
                        [id] => 5
                        [order] => 2
                        [title] => 
                    )

                [2] => Array
                    (
                        [id] => 781
                        [order] => 3
                        [title] => 
                    )
            )

2nd array with sorted keys of 1st array.

$sort = array
    (
        [2] => 2
        [0] => 0
        [1] => 1
    )

You could use array_map for that:

$arr = array_map(function($val) use($chunk){
    return $chunk[$val];
}, $sort);

This is the output:

Array
(
    [2] => Array
        (
            [id] => 781
            [order] => 3
            [title] => 
        )

    [0] => Array
        (
            [id] => 212
            [order] => 1
            [title] => fdfdfdfdf
        )

    [1] => Array
        (
            [id] => 5
            [order] => 2
            [title] => 
        )

)

Now, if you want the keys to be 0,1,2..., you can use array_values, after mapping:

$arr = array_values($arr);

And the output:

Array
(
    [0] => Array
        (
            [id] => 781
            [order] => 3
            [title] => 
        )

    [1] => Array
        (
            [id] => 212
            [order] => 1
            [title] => fdfdfdfdf
        )

    [2] => Array
        (
            [id] => 5
            [order] => 2
            [title] => 
        )

)

Of course, there is no function for this. You'll have to do something similar

<?php
$chunk  = [
    // data
];
$sorted = [];
$sort   = [
    // ordered keys
];

foreach($sort as $keyToFind) {
    foreach($chunk as $arrayElement) {
        if($arrayElement['id'] == $keyToFind)) {
            $sorted[$keyToFind] = $arrayElement;
        }
    }
}

As you can see, this is a bit time and ressources consumming because of the two imbricated foreaches. Let's hope your arrays are not so big