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

  • SEARCH
  • Home
  • 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 7076051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:15:06+00:00 2026-05-28T06:15:06+00:00

I have combined two arrays using array_merge, and now need to sort them alphabetically.

  • 0

I have combined two arrays using array_merge, and now need to sort them alphabetically. Problem is I need to compare two different keys,

e.g.

[0] => stdClass Object
(
[id] => 16
[title] => Oranges
)

[1] => stdClass Object
(
[id] => 23
[title] => Apples
)

[2] => stdClass Object
(
[id] => 16
[name] => Bananas
)

I need to compare ‘name’ properties with each other, as well as with ‘title’ properties, then sort those alphabetically, (ignoring the id) so that my result would be:

Apples
Bananas
Oranges

However, the function I have here:

function compareItems($a, $b)
{
if ( $a->title < $b->title ) return -1;
if ( $a->title > $b->title ) return 1;
if ( $a->name < $b->name ) return -1;
if ( $a->name > $b->name ) return 1;
return 0;
}

usort($array, "compareItems");

…doesn’t combine, but sorts one set of properties (title) then the other (name), thus giving me:

Apples
Oranges
Bananas

I’m hoping I can amend this function to merge the two together – I’m not really a PHP programmer, so any help is much appreciated!

Thanks in advance.

  • 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-28T06:15:06+00:00Added an answer on May 28, 2026 at 6:15 am

    The comparison function I have below should work for you:

    function compareItems($a, $b)
    {
        $x = isset($a->title) ? $a->title : $a->name;
        $y = isset($b->title) ? $b->title : $b->name;
        return strcmp($x, $y);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays A and B . How do I combine them into
I have two arrays I want to combine. I need to take the values
Scenario I have two WCF Services combined in a single App.Config file. I can't
I have two PNGs that I am trying to combine into a PDF using
Do you have any suggestions with my problem. I need to use get and
I have a query that I want to sort alphabetically, but the trick is
I have two arrays of values that I would like to combine - but
I am trying to compare two byte arrays in memory and produce a data
I have two arrays of objects. I would like iterate over each of these
I have two Numpy record arrays that have exactly the same fields. What is

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.