Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3670210
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:20:12+00:00 2026-05-19T02:20:12+00:00

I have two PHP arrays like so: Array of X records containing the ID

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T02:20:13+00:00Added an answer on May 19, 2026 at 2:20 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays in PHP. The first array ($author_array) is comprised of user_ids
I have two arrays of animals (for example). $array = array( array( 'id' =>
I have two arrays of values that I would like to combine - but
I have two arrays: $Forms and $formsShared . <?php foreach ($Forms as $r): ?>
I have two PHP scripts, both using the same session by calling session_name('MySessID') .
I have two PHP files that I need to link. How can I link
I have a PHP file, Test.php, and it has two functions: <?php echo displayInfo();
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two databases, each containing email addresses of users. I've written a script
I have two arrays one with OS's such as Ubuntu and Windows and another

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.