7

I have two PHP arrays like so:

  1. Array of X records containing the ID of Wordpress posts (in a particular order)
  2. Array of Wordpress posts

The two arrays look something like this:

Array One (Sorted Custom Array of Wordpress Post IDs)

Array (  
  [0] => 54
  [1] => 10
  [2] => 4
)

Array Two (Wordpress Post Array)

Array ( 
    [0] => stdClass Object
        (
            [ID] => 4
            [post_author] => 1
    )
    [1] => stdClass Object
        (
            [ID] => 54
            [post_author] => 1
    )
    [2] => stdClass Object
        (
            [ID] => 10
            [post_author] => 1
    )
)

I would like to sort the array of wordpress posts with the order of the ID's in the first array.

edit: The server is running PHP Version 5.2.14

0

3 Answers 3

10

This should be quite easy using usort, which sorts the array using a user-defined comparison function. The result might look something like this:

usort($posts, function($a, $b) use ($post_ids) {
    return array_search($a->ID, $post_ids) - array_search($b->ID, $post_ids);
});

Note that this solution, since it uses anonymous functions and closures, requires PHP 5.3.


One easy solution for this pre-5.3 (the dark ages!) is to do this with a quick loop and then ksort:

$ret = array();
$post_ids = array_flip($post_ids);
foreach ($posts as $post) {
    $ret[$post_ids[$post->ID]] = $post;
}
ksort($ret);
Sign up to request clarification or add additional context in comments.

9 Comments

I was typing this exact same solution, +1. PS: i think you need to array_flip $post_ids before and after this function.
@Richard Tuin That would be one solution -- the other is to use array_search, as I was in the middle of correcting my post to use.
This looks like a very elegant solution, however, when I try and implement this, I'm getting confused to what the function is supposed to do? If I copy it straight from here I get : Parse error: syntax error, unexpected T_FUNCTION ---- I feel like I should be creating a function to do the comparison? (excuse the misunderstanding as well... I'm no doubt having a retard moment)... edit: reading this php.net/manual/en/function.usort.php seems to make it a little clearer, but still stuck!
@Tisch $posts is your array of post objects; $post_ids is your array of ID values. To understand usort, it's probably easiest to read the manual page that I've linked to in my answer. If you are still uncertain, perhaps you could clarify what you're unsure about? This solution requires you have PHP 5.3 -- I'll edit my answer to reflect this.
You could use array_flip() instead of array_search for the pre 5.3 solution, which would be much better for sorting a large list
|
2

You could create a nested looping mechanism to match up the order and ids and rebuild a new post array.

$new_post_array = array();

foreach($id_array as $id) {          //loop through custom ordered ids

    foreach($post_array as $post) {  //for every id loop through posts

        if($id == $post->ID){         //and when the custom ordered id matches the post->ID

            new_array[] = $post       //push the post on the new array

        }

    }

}

3 Comments

Making a copy of both the arrays (that's what foreach does) is unneccesary here.
Thankyou for your answer. This does the job, however seems a little sluggish? I've accepted the answer anyway because it does do what I am looking for, and works with PHP 5.2
sorry @jondavidjohn, I've had to switch to the faster solution. Thanks again.
2
$sortOrderMap = array_flip($postIds);

usort($posts, function($postA, $postB) use ($sortOrderMap) {
    return $sortOrderMap[$postA->ID] - $sortOrderMap[$postB->ID];
});

You can simply subtract b from a instead of a from b to sort the other direction

1 Comment

thankyou for your answer as well... The solution above is very similar so I'm going to pursue that one if possible. Looks like PHP 5.2 is stopping me though :(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.