Skip to content

Instantly share code, notes, and snippets.

@pablo-sg-pacheco
Last active September 23, 2016 17:02
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save pablo-sg-pacheco/76ffb123a7c30c23f5cc313f7e586809 to your computer and use it in GitHub Desktop.
Order an multidimensional array based on another array
<?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