0

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
    )
AMit SiNgh
  • 325
  • 4
  • 17
  • what output you want . give more details for your question – Kruti Aparnathi Jun 07 '17 at 13:09
  • Possible [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). How are you getting the 2nd array? Maybe at that moment you can already sort 1st array. – FirstOne Jun 07 '17 at 13:29
  • You could go with [usort](http://php.net/manual/en/function.usort.php), like so: [https://3v4l.org/MIJN6](https://3v4l.org/MIJN6). If that's the case, so maybe a dup?: [**PHP Sort Array By SubArray Value**](https://stackoverflow.com/questions/2477496/php-sort-array-by-subarray-value) – FirstOne Jun 07 '17 at 13:42

2 Answers2

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

)
FirstOne
  • 6,033
  • 7
  • 26
  • 45
  • Thanks, This is what I want. – AMit SiNgh Jun 08 '17 at 05:08
  • can u please explain what anonymous function with use keyword doing. – AMit SiNgh Jun 08 '17 at 05:16
  • @amitsingh array_map calls a callback for each element in a given array. That's in the docs. Now, the `use` part is there because, since we are inside a function, it's a different scope, so technically we don't have access to outside variables. When we add `use`, that given variable becomes accessible. [Callback function using variables calculated outside of it](https://stackoverflow.com/questions/4588714/php-callback-function-using-variables-calculated-outside-of-it) – FirstOne Jun 08 '17 at 10:11
0

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