I have this unconventional need to sort an array by an order defined in a different array. Let me elaborate with a simplified example:
$myArr = ['Dog', 'Cat', 'Giraffe', 'Shark'];
$orderArr = ['Giraffe', 'Dog', 'Shark', 'Cat'];
$myArr
needs to be modified to have the order of $orderArr
. The task is so nonsensical as it seems. In reality $myArr
is an array of object. $orderArr
provides only a template for sorting - it holds only one propery of the objects of $myArr
.
Perhaps one way would be to use array_walk
:
array_walk($myArr, function($v, $k) {
...
}, $orderArr);
I have hard times soliving this puzzle.
It'd be great to see your take on it!
Important
This question is by no way similar to the one presented in "possible duplicate" comment and should not be marked as duplicate. In the one qoted in the comment, there is a question about how an array can be sorted by one of its values, and not be an external 'template array'.