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
    )
3
  • what output you want . give more details for your question Commented Jun 7, 2017 at 13:09
  • Possible XY Problem. How are you getting the 2nd array? Maybe at that moment you can already sort 1st array. Commented Jun 7, 2017 at 13:29
  • You could go with usort, like so: https://3v4l.org/MIJN6. If that's the case, so maybe a dup?: PHP Sort Array By SubArray Value Commented Jun 7, 2017 at 13:42

2 Answers 2

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

)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, This is what I want.
can u please explain what anonymous function with use keyword doing.
@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
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.