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

Merge array to output an array with only unique values

我有 2 个数组需要合并以创建另一个具有唯一值的数组。数组 2 是可能的文件版本的完整列表,数组 1 是文件的当前版本。如果数组 1 和数组 2 中的项目具有相同的 id 值,我想从数组 1 中取出项目。

数组 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'),
);

数组 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' => '')
);

我在尝试使用以下内容时不断收到错误消息:

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

这是一个错误:

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

第30行是print_r()

如何将这些数组与 id 的唯一值合并在一起?

array_unique() 被设计为默认比较二维数组。如果要比较多级数组,需要传入SORT_REGULAR作为array_unique()的第二个参数。

print_r(array_unique($fileInVersion, SORT_REGULAR));

array_unique() 不适用于多维数组。 对多维数组使用以下代码

$fileInVersion = array_merge($fileExists,$soft);
$unique_array = array_map("unserialize", array_unique(array_map("serialize", $fileInVersion)));
print_r($unique_array);

首先使用 id 重建索引 $array2array_column 可以让这变得简单:

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

然后为 $array1 中具有匹配 'id' 键的每个值覆盖 $array2 中的键。

foreach ($array1 as $key => $value) {
    $array2[$value['id']] = $value;
}