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

5
  • print array like this echo '<pre>'; print_r($array); echo '</pre>'; and re-post array structure to make it readable Commented Jul 20, 2011 at 5:12
  • Nope, post the output of the code above posted on my first comment Commented Jul 20, 2011 at 5:24
  • PLease check again .. we have updated the results Commented Jul 20, 2011 at 5:34
  • Run this code snippet in your browser echo <pre>'; print_r($duplicates); echo '</pre>'; and post the output Commented Jul 20, 2011 at 5:36
  • I m beginner in using stackoverflow code design. Please check it, this is ok ... if you want to look into $duplicates then please look at the top of the codes. there is a array variable and its same as you want. Commented Jul 20, 2011 at 5:45

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Please look at the above question, we have updated result with your values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.