I have two simple arrays. One is an array of posts ($posts) and the other is an array of post ID's that should be on top of the stream, like they are featured ($featured). I am trying to find an efficient way to loop through them and if an ID is in both arrays, move it to the beginning of the $posts array, and the ones that are moved should be sorted ASC by $featured['order'], so for example, some posts ($posts):
Array
(
[ab77] => Status_Update Object
(
[id] => ab77
[title] => Status Update
)
[b4k7] => Status_Update Object
(
[id] => b4k7
[title] => Status Update
)
[4d55] => Status_Update Object
(
[id] => 4d55
[title] => Status Update
)
[13c5] => Status_Update Object
(
[id] => 13c5
[title] => Status Update
)
[3aa2] => Status_Update Object
(
[id] => 3aa2
[title] => Status Update
)
)
and then some posts which should be featured ($featured):
Array
(
[13c5] => Array
(
[id] => 13c5
[order] => 1
)
[3a71] => Array
(
[id] => 3a71
[order] => 2
)
[4d55] => Array
(
[id] => 4d55
[order] => 3
)
)
So the $posts array should end up sorted like this:
13c5 4d55 ab77 bk47 3aa2
How do I do this without a bunch of slow loops?
array_key_exists('13c5', $featured))etcuksortto sort using a user-written comparison function. The function should check whether both keys are in the featured array usingarray_key_exists. If they're both in or both not in, compare them with<.