Last active
September 23, 2016 17:02
-
-
Save pablo-sg-pacheco/76ffb123a7c30c23f5cc313f7e586809 to your computer and use it in GitHub Desktop.
Order an multidimensional array based on another array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
$terms = | |
Array | |
( | |
[2] => WP_Term Object | |
( | |
[term_id] => 6 | |
) | |
[1] => WP_Term Object | |
( | |
[term_id] => 5 | |
) | |
[0] => WP_Term Object | |
( | |
[term_id] => 3 | |
) | |
) | |
$originalTerms = | |
Array | |
( | |
[0] => WP_Term Object | |
( | |
[term_id] => 6 | |
) | |
[1] => WP_Term Object | |
( | |
[term_id] => 5 | |
) | |
[2] => WP_Term Object | |
( | |
[term_id] => 3 | |
) | |
) | |
*/ | |
usort($terms, function($a, $b) use ($originalTerms) { | |
$keyA = array_search($a->term_id, array_column(array_map(function($o){return (array)$o;},$originalTerms),'term_id')); | |
$keyB = array_search($b->term_id, array_column(array_map(function($o){return (array)$o;},$originalTerms),'term_id')); | |
return ($keyA<$keyB) ? -1 : 1; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment