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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:43:03+00:00 2026-05-23T23:43:03+00:00

We Would like to know, how can we use array_diff() in two arrays. Please

  • 0

We Would like to know, how can we use array_diff() in two arrays. Please look at below.


    $duplicates=  Array ( [0] => Array ( [url] => http://www.cheapohotels.co.uk/resources /index_car_hire.html [ip] => 77.92.74.100 [status] => Duplicate ) [1] => Array ( [url] => http://www.villasinprovence.co.uk/resources/index_car_hire.html [ip] => 77.92.74.100 [status] => Duplicate ) [2] => Array ( [url] => http://www.mobiz.co.uk/resources/index_recreation_autos.html [ip] => 77.92.74.100 [status] => Duplicate ) [3] => Array ( [url] => http://www.holidaysoffersuk.co.uk/resources/index_car_hire.html [ip] => 77.92.74.100 [status] => Duplicate ) [4] => Array ( [url] => http://www.ticketsforsaleuk.co.uk/resources/index_companies_automotive.html [ip] => 77.92.74.100 [status] => Duplicate ) [5] => Array ( [url] => http://www.holidayparkfinder.co.uk/resources/index_car_hire.html [ip] => 213.171.218.208 [status] => Duplicate ) [6] => Array ( [url] => http://www.themeparkhotels.co.uk/resources/index_car_hire.html [ip] => 213.171.218.10 [status] => Duplicate ) ) 

    $Total_val =  Array ( [0] => Array ( [url] => http://www.cheapohotels.co.uk/resources/index_car_hire.html [ip] => 77.92.74.100 [status] => Unique ) [1] => Array ( [url] => http://www.holidayparkfinder.co.uk/resources/index_car_hire.html [ip] => 213.171.218.208 [status] => Unique ) [2] => Array ( [url] => http://www.themeparkhotels.co.uk/resources/index_car_hire.html [ip] => 213.171.218.10 [status] => Unique ) [3] => Array ( [url] => http://www.villasinprovence.co.uk/resources/index_car_hire.html [ip] => 77.92.74.100 [status] => Unique ) [4] => Array ( [url] => http://usedmotorcycleaccessories.com/resources/ [ip] => 174.37.15.56 [status] => Unique ) [5] => Array ( [url] => http://aftermarket-motorcycleparts.com/resources/ [ip] => 50.23.134.232 [status] => Unique ) [6] => Array ( [url] => http://cheaponlinemotorcycleparts.com/resources/ [ip] => 173.192.128.240 [status] => Unique ) [7] => Array ( [url] => http://usedcaronlinereviews.com/resources/ [ip] => 50.23.153.112 [status] => Unique ) [8] => Array ( [url] => http://cartransportationreviews.com/resources/ [ip] => 50.23.132.48 [status] => Unique ) [9] => Array ( [url] => http://vehicleaccidentinsurancequotes.com/ [ip] => 65.75.176.111 [status] => Unique ) [10] => Array ( [url] => http://www.netbizcorp.com/linkmachine/resources/resources_recreation_autos.html [ip] => 74.209.213.8 [status] => Unique ) [11] => Array ( [url] => http://www.mobiz.co.uk/resources/index_recreation_autos.html [ip] => 77.92.74.100 [status] => Unique ) [12] => Array ( [url] => http://www.holidaysoffersuk.co.uk/resources/index_car_hire.html [ip] => 77.92.74.100 [status] => Unique ) [13] => Array ( [url] => http://www.ticketsforsaleuk.co.uk/resources/index_companies_automotive.html [ip] => 77.92.74.100 [status] => Unique ) ) 
echo <pre>'; print_r($duplicates); echo '</pre>';

In the above arrays, having values(Duplicate and Unique). by program default $Total_val stored and mentioned unique status in the values. but these array have not the unique values, to getting the unique values , we have to subtracts from $duplicates. And we doing same in php we are not getting the exact values… subtracting by IP not for other values. The main reason behind this. we have to make a new array with unique ip’s like =


    $new_array = array_diff($Total_val, $duplicates);
    //the output for this function is
      Array ( ) ;  // showing Blank Result.
    $new_array = array_diff_assoc($array1, $dup);
    //echo result, then the output is 

    Array ( [7] => Array ( [url] => http://usedcaronlinereviews.com/resources/ [ip] => 50.23.153.112 [status] => Unique ) [8] => Array ( [url] => http://cartransportationreviews.com/resources/ [ip] => 50.23.132.48 [status] => Unique ) [9] => Array ( [url] => http://vehicleaccidentinsurancequotes.com/ [ip] => 65.75.176.111 [status] => Unique ) [10] => Array ( [url] => http://www.netbizcorp.com/linkmachine/resources/resources_recreation_autos.html [ip] => 74.209.213.8 [status] => Unique ) [11] => Array ( [url] => http://www.mobiz.co.uk/resources/index_recreation_autos.html [ip] => 77.92.74.100 [status] => Unique ) [12] => Array ( [url] => http://www.holidaysoffersuk.co.uk/resources/index_car_hire.html [ip] => 77.92.74.100 [status] => Unique ) [13] => Array ( [url] => http://www.ticketsforsaleuk.co.uk/resources/index_companies_automotive.html [ip] => 77.92.74.100 [status] => Unique ) ) 

You can check the Final output from this function “array_diff_assoc()” , its working. but its not showing the exact value. like you can see the IP 77.92.74.100 . its repeated three time and still its in unique status.

Array ( [0] => Array ( [url] => http://www.cheapohotels.co.uk/resources/index_car_hire.html [ip] => 77.92.74.100 [status] => Unique ) [1] => Array ( [url] => http://www.holidayparkfinder.co.uk/resources/index_car_hire.html [ip] => 213.171.218.208 [status] => Unique ) [2] => Array ( [url] => http://www.themeparkhotels.co.uk/resources/index_car_hire.html [ip] => 213.171.218.10 [status] => Unique ) [3] => Array ( [url] => http://www.villasinprovence.co.uk/resources/index_car_hire.html [ip] => 77.92.74.100 [status] => Unique ) [4] => Array ( [url] => http://usedmotorcycleaccessories.com/resources/ [ip] => 174.37.15.56 [status] => Unique ) [5] => Array ( [url] => http://aftermarket-motorcycleparts.com/resources/ [ip] => 50.23.134.232 [status] => Unique ) [6] => Array ( [url] => http://cheaponlinemotorcycleparts.com/resources/ [ip] => 173.192.128.240 [status] => Unique ) [7] => Array ( [url] => http://usedcaronlinereviews.com/resources/ [ip] => 50.23.153.112 [status] => Unique ) [8] => Array ( [url] => http://cartransportationreviews.com/resources/ [ip] => 50.23.132.48 [status] => Unique ) [9] => Array ( [url] => http://vehicleaccidentinsurancequotes.com/ [ip] => 65.75.176.111 [status] => Unique ) [10] => Array ( [url] => http://www.netbizcorp.com/linkmachine/resources/resources_recreation_autos.html [ip] => 74.209.213.8 [status] => Unique ) [11] => Array ( [url] => http://www.mobiz.co.uk/resources/index_recreation_autos.html [ip] => 77.92.74.100 [status] => Unique ) [12] => Array ( [url] => http://www.holidaysoffersuk.co.uk/resources/index_car_hire.html [ip] => 77.92.74.100 [status] => Unique ) [13] => Array ( [url] => http://www.ticketsforsaleuk.co.uk/resources/index_companies_automotive.html [ip] => 77.92.74.100 [status] => Unique ) )

giving me this when i tried this function

function cmp($a, $b) {
return strcmp($a['status'], $b['status']);
}
$diff = array_udiff($array1, $dup, 'cmp');

Status remain same “unique to all the values”, i just want to mention accordingly. like if the duplicate ip is available then the status will be Duplicate. same as for Unique.

Please give me the suggestion for this program.

Thanks

ROD

  • 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-23T23:43:04+00:00Added an answer on May 23, 2026 at 11:43 pm

    The problem is how array_diff determines the equality of two values:

    Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same.

    The problem here is that converting arrays to string always yields "Array":

    Arrays are always converted to the string “Array”; […]

    So every array value in your first array is considered a duplicate of any other array value of the other array. And as array_diff does not return the symmetric difference but A \ B (i.e. A minus B) and A has more elements than B, you are just getting $Total_val without the first seven items back.

    Now to get the right results, you need to use a different comparison function other that comparing the string representations of each item as array_diff does. You can use the array_udiff to do so:

    function cmpByIp($a, $b) {
        return strcmp($a['ip'], $b['ip']);
    }
    $diff = array_udiff($Total_val, $duplicates, 'cmpByIp');
    

    This compares your array items just by ip.

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

Sidebar

Related Questions

I would like to know if I can use the following JQuery function on
I would like to know how can I use tinyint in SQL Server 2005
I would like to know how can I use jQuery to select multiple items
I have a generator and I would like to know if I can use
I would like to know where i can find my callback url in ci?
I would like to know how can I query an array of objects. For
i would like know some reference. I know i can googling it. but prefer
I would like to know where can I find the code which eclipse uses
I would like to know how Can I paas Page as Ref Parameter to
I would like to know how can i read a value of input text

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.