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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:20:25+00:00 2026-05-22T15:20:25+00:00

I have two nested arrays and need to find the difference between the reference

  • 0

I have two nested arrays and need to find the difference between the reference array and the data array. I am using array_dif_assoc function, and am unable to get the right difference, I am not sure why I am unable to get it. Could somebody help me out if I am doing some mistake or if I have to do it recursively;

$allCoursesAvailable = array(array('id'=>0,'name'=>'Select-One'), array('id'=>1,'name'=>'course1'),array('id'=>1,'name'=>'course2'),array('id'=>3,'name'=>'course3'));

$allCoursesforUser = array(array('id'=>0,'name'=>'Select-One'), array('id'=>1,'name'=>'course1'),array('id'=>4,'name'=>'course4'),array('id'=>5,'name'=>'course5'),array('id'=>6,'name'=>'course4'));

echo '<pre>';print_r(array_diff_assoc($allCoursesAvailable,$allCoursesforUser));

I am getting an empty array with this. When I use array_diff_assoc(), I should have got the arrays carrying course2 and course3, as they are not part of the 2nd array.

Am I missing some logic on the PHP end?

  • 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-22T15:20:25+00:00Added an answer on May 22, 2026 at 3:20 pm

    You could always start by reading the PHP manual.

    For array_diff_assoc (on http://php.net/manual/en/function.array-diff-assoc.php) it is said that this function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using, for example, array_diff_assoc($array1[0], $array2[0]);.

    Provided solution in user comments (this does work):

    55 dot php at imars dot com
    17-Mar-2009 03:09 I’ve worked on
    array_diff_assoc_recursive() mentioned
    by chinello at gmail dot com and I
    think it might be worth mentioning
    here. I wrote a dozen test cases and
    it seems to be holding up pretty well.

    <?php
    // dwarven Differences:
    // * Replaced isset() with array_key_exists() to account for keys with null contents
    
    // 55 dot php at imars dot com Differences:
    // Key differences:
    // * Removed redundant test;
    // * Returns false bool on exact match (not zero integer);
    // * Use type-precise comparison "!==" instead of loose "!=";
    // * Detect when $array2 contains extraneous elements;
    // * Returns "before" and "after" instead of only "before" arrays on mismatch.
    
    function array_compare($array1, $array2) {
        $diff = false;
        // Left-to-right
        foreach ($array1 as $key => $value) {
            if (!array_key_exists($key,$array2)) {
                $diff[0][$key] = $value;
            } elseif (is_array($value)) {
                 if (!is_array($array2[$key])) {
                        $diff[0][$key] = $value;
                        $diff[1][$key] = $array2[$key];
                 } else {
                        $new = array_compare($value, $array2[$key]);
                        if ($new !== false) {
                             if (isset($new[0])) $diff[0][$key] = $new[0];
                             if (isset($new[1])) $diff[1][$key] = $new[1];
                        };
                 };
            } elseif ($array2[$key] !== $value) {
                 $diff[0][$key] = $value;
                 $diff[1][$key] = $array2[$key];
            };
     };
     // Right-to-left
     foreach ($array2 as $key => $value) {
            if (!array_key_exists($key,$array1)) {
                 $diff[1][$key] = $value;
            };
            // No direct comparsion because matching keys were compared in the
            // left-to-right loop earlier, recursively.
     };
     return $diff;
    };
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays of animals (for example). $array = array( array( 'id' =>
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have a div with two nested divs inside, the (float:left) one is the
What I'm talking about here are nested classes. Essentially, I have two classes that
I have two applications written in Java that communicate with each other using XML
I have two identical tables and need to copy rows from table to another.
I have two elements: <input a> <input b onclick=...> When b is clicked, I
I have two classes, and want to include a static instance of one class
I have two threads, one needs to poll a bunch of separate static resources
I have two spreadsheets... when one gets modified in a certain way I want

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.