dpevsxjn809817
dpevsxjn809817
2016-07-29 20:44

合并数组以输出仅具有唯一值的数组

已采纳

I have 2 arrays that I need merged to create another array with unique values. Array 2 is the entire list of possible file versions, and Array 1 is the current versions of the files. If items from Array 1 and Array 2 have the same id value, I want to take the item from Array 1.

Array 1

$array1 = array(
    array('id' => '8','file_extension_id' => '8','extension' => '','name' => 'GS','file_version' => '1.0.2'),
    array('id' => '2','file_extension_id' => '2','extension' => 'gif', 'name' => 'GIF','file_version' => '1.0.2'),
    array('id' => '1','file_extension_id' => '1','extension' => 'png','name' => 'PNG','file_version' => '1.0.2'),
    array('id' => '19','file_extension_id' => '19','extension' => 'jpg','name' => 'JPG','file_version' => '1.0.2'),
    array('id' => '20','file_extension_id' => '20','extension' => 'pdf','name' => 'PDF','file_version' => '1.0.2'),
);

Array 2

$array2 = array(
    array('id' => '1','file_extension_id' => '','extension' => 'png','name' => 'PNG','file_version' => ''),
    array('id' => '2','file_extension_id' => '','extension' => 'gif','name' => 'GIF','file_version' => ''),
    array('id' => '3','file_extension_id' => '','extension' => 'doc','name' => 'Word 2003','file_version' => ''),
    array('id' => '4','file_extension_id' => '','extension' => 'docx','name' => 'Word 2007+','file_version' => ''),
    array('id' => '5','file_extension_id' => '','extension' => 'xlsx','name' => 'Excel 2010+','file_version' => ''),
    array('id' => '6','file_extension_id' => '','extension' => 'docx','name' => 'Word 2010+','file_version' => ''),
    array('id' => '7','file_extension_id' => '','extension' => 'numbers','name' => 'Mac Numbers','file_version' => ''),
    array('id' => '8','file_extension_id' => '','extension' => '','name' => 'GS','file_version' => ''),
    array('id' => '9','file_extension_id' => '','extension' => '','name' => 'Google Docs','file_version' => ''),
    array('id' => '10','file_extension_id' => '','extension' => 'ots','name' => 'OpenOffice.org Calc','file_version' => ''),
    array('id' => '11','file_extension_id' => '','extension' => 'ott','name' => 'OpenOffice.org Writer','file_version' => ''),
    array('id' => '12','file_extension_id' => '','extension' => 'xlsx','name' => 'Excel for iPad','file_version' => ''),
    array('id' => '13','file_extension_id' => '','extension' => 'xlsm','name' => 'Excel 2007 Macros','file_version' => ''),
    array('id' => '14','file_extension_id' => '','extension' => 'xlsm','name' => 'Excel 2010 Macros','file_version' => ''),
    array('id' => '15','file_extension_id' => '','extension' => 'xlsx','name' => 'Excel 2013+','file_version' => ''),
    array('id' => '16','file_extension_id' => '','extension' => 'xlsm','name' => 'Excel 2013 Macros','file_version' => ''),
    array('id' => '17','file_extension_id' => '','extension' => 'xlsx','name' => 'Excel 2016','file_version' => ''),
    array('id' => '18','file_extension_id' => '','extension' => 'xlsm','name' => 'Excel 2016 Macros','file_version' => ''),
    array('id' => '19','file_extension_id' => '','extension' => 'jpg','name' => 'JPG','file_version' => ''),
    array('id' => '20','file_extension_id' => '','extension' => 'pdf','name' => 'PDF','file_version' => ''),
    array('id' => '21','file_extension_id' => '','extension' => 'xlsx','name' => 'Excel for iPhone','file_version' => ''),
    array('id' => '22','file_extension_id' => '','extension' => 'xlsx','name' => 'Excel for Android','file_version' => ''),
    array('id' => '23','file_extension_id' => '','extension' => 'xls','name' => 'Excel 2003 Macros','file_version' => ''),
    array('id' => '24','file_extension_id' => '','extension' => 'docx','name' => 'Word 2013+','file_version' => ''),
    array('id' => '25','file_extension_id' => '','extension' => 'docx','name' => 'Word 2016','file_version' => '')
);

I keep getting an error when trying to use the following:

$fileInVersion = array_merge($fileExists,$soft);
print_r(array_unique($fileInVersion));

Here is an error:

Notice: Array to string conversion in /file.php on line 30

The line 30 is print_r()

How can I merge these arrays together with unique values for id?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • dtzhfb4869 dtzhfb4869 5年前

    First reindex $array2 using id. array_column can make this easy:

    $array2 = array_column($array2, null, 'id');
    

    Then overwrite the keys in $array2 for each value in $array1 that has a matching 'id' key.

    foreach ($array1 as $key => $value) {
        $array2[$value['id']] = $value;
    }
    
    点赞 评论 复制链接分享
  • duannian7116 duannian7116 5年前

    array_unique() is not intended to work on multi dimensional arrays. Use the below code for multi dimensional arrays

    $fileInVersion = array_merge($fileExists,$soft);
    $unique_array = array_map("unserialize", array_unique(array_map("serialize", $fileInVersion)));
    print_r($unique_array);
    
    点赞 评论 复制链接分享
  • dph19153 dph19153 5年前

    array_unique() is designed to compare 2 dimensional arrays by default. If you want to compare arrays with more than one level, you need to pass in SORT_REGULAR as the second parameter of array_unique().

    print_r(array_unique($fileInVersion, SORT_REGULAR));
    
    点赞 评论 复制链接分享