Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

two php arrays - sort one array with the value order of another

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.

I hope this makes sense, and thanks in advance of any help.

Tom

edit: The server is running PHP Version 5.2.14

like image 769
Tisch Avatar asked Jan 09 '11 22:01

Tisch


People also ask

How do I sort two arrays in the same order?

Write a SortedMerge() function that takes two lists, each of which is unsorted, and merges the two together into one new list which is in sorted (increasing) order. SortedMerge() should return the new list.

How do you sort an array of associative arrays by the value of a given key in PHP?

The arsort() function sorts an associative array in descending order, according to the value. Tip: Use the asort() function to sort an associative array in ascending order, according to the value. Tip: Use the krsort() function to sort an associative array in descending order, according to the key.

Can you sort a multidimensional array?

Sorting a multidimensional array by element containing date. Use the usort() function to sort the array. The usort() function is PHP builtin function that sorts a given array using user-defined comparison function.


3 Answers

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);
like image 178
lonesomeday Avatar answered Nov 10 '22 00:11

lonesomeday


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

        }

    }

}
like image 42
jondavidjohn Avatar answered Nov 09 '22 23:11

jondavidjohn


$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

like image 24
goat Avatar answered Nov 10 '22 01:11

goat