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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:56:47+00:00 2026-05-13T23:56:47+00:00

How does array_diff() work? It obviously couldn’t work as follows: function array_diff($arraya, $arrayb) {

  • 0

How does array_diff() work? It obviously couldn’t work as follows:

function array_diff($arraya, $arrayb)
{
    $diffs = array();
    foreach ($arraya as $keya => $valuea)
    {
        $equaltag = 0;
        foreach ($arrayb as $valueb)     
        {
            if ($valuea == $valueb)
            {
                $equaltag =1;
                break;
            }
        }
        if ($equaltag == o)
        {
              $diffs[$keya]=$valuea;
        }

    }
    return $diffs;                          
}                                  //couldn't be worse than this

Does anyone know a better solution?

EDIT @animuson:

function array_diff($arraya, $arrayb)
{
    foreach ($arraya as $keya => $valuea)
    {
        if (in_array($valuea, $arrayb))
        {
            unset($arraya[$keya]);
        }
    }
    return $arraya;
}
  • 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-13T23:56:47+00:00Added an answer on May 13, 2026 at 11:56 pm

    UPDATE

    • see below for faster/better code.

    • array_diff behaviour is much better in php 5.3.4, but still ~10 times slower than Leo’s function.

    • also it’s worth noting that these functions are not strictly equivalent to array_diff since they don’t maintain array keys, i.e. my_array_diff(x,y) == array_values(array_diff(x,y)).

    /UPDATE

    A better solution is to use hash maps

    function my_array_diff($a, $b) {
        $map = $out = array();
        foreach($a as $val) $map[$val] = 1;
        foreach($b as $val) if(isset($map[$val])) $map[$val] = 0;
        foreach($map as $val => $ok) if($ok) $out[] = $val;
        return $out;
    }
    
    $a = array('A', 'B', 'C', 'D');
    $b = array('X', 'C', 'A', 'Y');
    
    print_r(my_array_diff($a, $b)); // B, D
    

    benchmark

    function your_array_diff($arraya, $arrayb)
    {
        foreach ($arraya as $keya => $valuea)
        {
            if (in_array($valuea, $arrayb))
            {
                unset($arraya[$keya]);
            }
        }
        return $arraya;
    }
    
    $a = range(1, 10000);
    $b = range(5000, 15000);
    
    shuffle($a);
    shuffle($b);
    
    $ts = microtime(true);
    my_array_diff($a, $b);
    printf("ME =%.4f\n", microtime(true) - $ts);
    
    $ts = microtime(true);
    your_array_diff($a, $b);
    printf("YOU=%.4f\n", microtime(true) - $ts);
    

    result

    ME =0.0137
    YOU=3.6282
    

    any questions? 😉

    and, just for fun,

    $ts = microtime(true);
    array_diff($a, $b);
    printf("PHP=%.4f\n", microtime(true) - $ts);
    

    result

    ME =0.0140
    YOU=3.6706
    PHP=19.5980
    

    that’s incredible!

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

Sidebar

Related Questions

I've read several discussions of passing char * in C. stackoverflow: passing-an-array-of-strings-as-parameter-to-a-function-in-c stackoverflow: how-does-an-array-of-pointers-to-pointers-work
Is there any function that does the inverse operation of array_diff() ? I mean,
Let's say I have a function called MyFunction(int myArray[][]) that does some array manipulations.
Why does the array a not get initialized by global variable size ? #include<stdio.h>
When you do Something.find(array_of_ids) in Rails, the order of the resulting array does not
Does Java guarantee array initialization? Let's say I use the code char[] uuid =
Native Array in Emberjs does not support deep copy? I saw it just returning
Does anyone know how to store a 1-dimensional Matlab array in a single field
Does someone know why i never get the first value of my array? it
Does Scala have a version of Rubys' each_slice from the Array class?

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.