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
echo '<pre>'; print_r($array); echo '</pre>';
and re-post array structure to make it readable – Shakti Singh Jul 20 '11 at 5:12echo <pre>'; print_r($duplicates); echo '</pre>';
and post the output – Shakti Singh Jul 20 '11 at 5:36