0


Hi, can anyone help me to sort a multidimensional array by another array?

Array 1:

Array(
    [0] => Array( 
        'id' => '58e9f01ff291c'
    ),
    [1] => Array( 
        'id' => '58e9f0590d6c0' 
    ),
    [2] => Array(
        'id' => '58ea5274219ac
    )
)


Array 2:

Array('58e9f0590d6c0', '58ea5274219ac', '58e9f01ff291c')

The second array contains the IDs of the first array and I would like to sort the first array by the IDs in the second array. How can I do this?

Best wishes and thanks for any help
Canned


Update:
I figured it out:

$return = array();
foreach($array2 as $sortId) {
    foreach ($array1 as $subKey => $subArray) {
        if ($subArray['id'] == $sortId) {
            $return[] = $array1[$subKey];
            break;
        }
    }
}
3
  • Have you tried array_multisort? php.net/manual/en/function.array-multisort.php Commented Apr 9, 2017 at 17:20
  • I know array_multisort but how can I combine it with Array 2? Best wishes, Canned Commented Apr 9, 2017 at 17:24
  • $a = Array('58e9f0590d6c0', '58ea5274219ac', '58e9f01ff291c'); $a = array_map(function($i) { return array('id' => $i); }, $a); Commented Apr 9, 2017 at 18:01

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.