PHP数组根据值合并和区别

时间:2011-07-20 05:06:03

标签: php arrays array-merge

我们想知道,我们如何在两个数组中使用array_diff()。请看下面。


    $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>';

在上面的数组中,有值(Duplicate和Unique)。按程序默认存储$ Total_val并在值中提及唯一状态。但是这些数组没有唯一值,要获得唯一值,我们必须从$ duplicates中减去。我们在php中做同样的事情我们没有得到确切的值...通过IP减去其他值。这背后的主要原因。我们必须创建一个具有唯一ip的新数组,如=


    $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 ) ) 

你可以检查这个函数“array_diff_assoc()”的最终输出,它的工作原理。但它没有显示确切的价值。就像你可以看到IP 77.92.74.100。它重复了三次,仍处于独特状态。

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 ) )

当我尝试这个功能时给我这个

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

状态保持相同“所有值都是唯一的”,我只想相应提及。就像重复的ip可用,那么状态将是Duplicate。与Unique相同。

请给我这个计划的建议。

谢谢

ROD

1 个答案:

答案 0 :(得分:1)

问题是array_diff如何确定两个值的相等性:

  

注意:当且仅当(string) $elem1 === (string) $elem2时,才认为两个元素相等。用文字表示:当字符串表示相同时。

这里的问题是converting arrays to string始终会产生"Array"

  

数组总是转换为字符串“Array”; [...]

因此,第一个数组中的每个数组值都被视为与另一个数组的任何其他数组值重复。由于array_diff不会返回symmetric difference A \ B (即 A 减去 B < / em>)和 A 的元素多于 B ,你只需要$Total_val而不返回前七个项目。

现在要获得正确的结果,您需要使用不同的比较函数,比较每个项目的字符串表示形式array_diff。您可以使用array_udiff执行此操作:

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

这只是通过 ip 比较您的数组项目。