如何组合 - 覆盖数组中的键值对

时间:2017-03-07 12:52:47

标签: php arrays

我有一个包含一些条目的数组。我用“dummydata”替换了真实数据。但是“dummydata”与array [0]和array [1]的值相同。正如您所看到的那样,某些键存在一些差异:

Array
(
    [0] => Array
        (
            [contact_id] => dummydata
            [customer_code] => dummydata
            [customer_name] => dummydata
            [territory] => dummydata       
            [firstname] => dummydata
            [lastname] => dummydata
            [contact_type_description] => dummydata
            [contact_is_primary] => 1
            [mailing_street] => dummydata
            [mailing_postal_code] => dummydata
            [mailing_city] => dummydata
            [mailing_country] => dummydata
            [email] => dummydata
            [mobile_phone] => dummydata
            [phone] => dummydata
            [is_processed] => 0
            [exact_email_found] => 0
            [half_email_found] => 0
            [possible_by_postal_code] => 1 // * Keep this line but look below
            [found_first_name] => 0
            [found_last_name] => 0 // * Keep this line but look below
        )

    [1] => Array
        (
            [contact_id] => dummydata
            [customer_code] => dummydata
            [customer_name] => dummydata
            [territory] => dummydata
            [firstname] => dummydata
            [lastname] => dummydata
            [contact_type_description] => dummydata
            [contact_is_primary] => 1
            [mailing_street] => dummydata
            [mailing_postal_code] => dummydata
            [mailing_city] => dummydata
            [mailing_country] => dummydata
            [email] => dummydata
            [mobile_phone] => 
            [phone] => dummydata
            [is_processed] => 0
            [exact_email_found] => 0
            [half_email_found] => 0
            [possible_by_postal_code] => 0 // * HERE is the difference
            [found_first_name] => 0
            [found_last_name] => 1 //  * HERE is the difference with the above
        )

    [2] => Array // No need to look it's just to show that array continues
        (
            [contact_id] => dummydata-different
            [customer_code] => dummydata-different
            [customer_name] => dummydata-different
            [territory] => dummydata-different  
            [firstname] => dummydata-different
            [lastname] => dummydata-different
            [contact_type_description] => dummydata-different
            [contact_is_primary] => 0
            [mailing_street] => dummydata-different
            [mailing_postal_code] => dummydata-different
            [mailing_city] => dummydata-different
            [mailing_country] => dummydata-different
            [email] => 
            [mobile_phone] => 
            [phone] => 
            [is_processed] => 0
            [exact_email_found] => 0
            [half_email_found] => 0
            [possible_by_postal_code] => 0
            [found_first_name] => 1
            [found_last_name] => 0
        )

如何使上面的数组合并所有相同的数据,但覆盖不同的数据。例如,我想要完成的结果:

Array
(
    [0] => Array
        (
            [contact_id] => dummydata
            [customer_code] => dummydata
            [customer_name] => dummydata
            [territory] => dummydata       
            [firstname] => dummydata
            [lastname] => dummydata
            [contact_type_description] => dummydata
            [contact_is_primary] => 1
            [mailing_street] => dummydata
            [mailing_postal_code] => dummydata
            [mailing_city] => dummydata
            [mailing_country] => dummydata
            [email] => dummydata
            [mobile_phone] => dummydata
            [phone] => dummydata
            [is_processed] => 0
            [exact_email_found] => 0
            [half_email_found] => 0
            [possible_by_postal_code] => 1 // * This becomes one from the merging
            [found_first_name] => 0
            [found_last_name] => 1 // * This becomes one from the merging
        )


    [1] => Array
        (
            [contact_id] => dummydata-different
            [customer_code] => dummydata-different
            [customer_name] => dummydata-different
            [territory] => dummydata-different  
            [firstname] => dummydata-different
            [lastname] => dummydata-different
            [contact_type_description] => dummydata-different
            [contact_is_primary] => 0
            [mailing_street] => dummydata-different
            [mailing_postal_code] => dummydata-different
            [mailing_city] => dummydata-different
            [mailing_country] => dummydata-different
            [email] => 
            [mobile_phone] => 
            [phone] => 
            [is_processed] => 0
            [exact_email_found] => 0
            [half_email_found] => 0
            [possible_by_postal_code] => 0
            [found_first_name] => 1
            [found_last_name] => 0
        )

用PHP提供的功能尝试了不同的东西,但这让我很困惑。

2 个答案:

答案 0 :(得分:0)

你可以这样做:

$result = array_map("unserialize", array_unique(array_map("serialize", $input_array)));

$input_array是具有重复值的数组。

所以这里基本上发生的是第一个数组被转换为单维数组,然后删除重复值,然后再将它转换为多维数组。

答案 1 :(得分:0)

我说保持简单(和快速)

foreach ($arr[0] as $k=>$v) {
    if ($v > $arr[1][$k]) $arr[1][$k] = $v;
}

array_shift($arr);

注意:如果您要比较的两个项目不在数组的开头,请使用unsetarray_values代替array_shift