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 7987711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:13:36+00:00 2026-06-04T12:13:36+00:00

Say I have two complex nested arrays in PHP, like these: $a = array(

  • 0

Say I have two complex nested arrays in PHP, like these:

$a = array(
    "x" => array(4, 5, 6),
    "y" => array("z" => "foo", "q" => "bar")
    );
$b = array(
    "y" => array("q" => "bar", "z" => "foo"), 
    "x" => array(4, 5, 6)
    );

(In this case, they’re decoded JSON data from different sources). Assume the contents can be arbitrarily nested, but will not contain any circular references.

What’s the most straightforward way to check if they are equal, ignoring key ordering? For example, the above two should compare equal. However, if $b["x"] were array(4, 6, 5) they would not be.

I could recursively ksort and compare the results, but I don’t really want to modify either operand, and this seems like something that might have a simple one-line solution I don’t know about. Is there anything out there?

  • 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-06-04T12:13:38+00:00Added an answer on June 4, 2026 at 12:13 pm

    The best way of doing this, is already mentioned by you. But you forgot 1 thing.

    1. Copy the arrays
    2. Sort the Arrays
    3. Compare the 2

    The same? Perfect. And you still have the original.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have two models looking like this: class ProductType(models.Model): product_type = models.CharField(max_length=100)
So I have two custom complex types like this (oversimplified for this example): public
I have two complex objects like Object1 and Object2 . They have around 5
Let's say I have table like this: id: PRIMARY INT price: INT (in dollars)
Say I have a unit test that wants to compare two complex for objects
Lets say I have two MySQL databases with some complex table structures. Neither database
Say I have a string like this: [1] <u>Degradation:</u> AGL, PGM1, PGM2, PGM3, PYGL,
Say we have two tables in an MS Access db: Service Users: | ID
Say I have two classes and have a requirement that the primary key property
Say I have two functions that expect ...rest parameters private function a(...myParams):void { trace(myParams.length);

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.