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

时间:2016-07-29 20:44:45

标签: php arrays multidimensional-array array-merge array-unique

我有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));

这是一个错误:

  

注意:第30行的/file.php中的数组到字符串转换

第30行是print_r()

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

3 个答案:

答案 0 :(得分:2)

array_unique()用于默认比较二维数组。如果要比较具有多个级别的数组,则需要将SORT_REGULAR作为array_unique()的第二个参数传递。

print_r(array_unique($fileInVersion, SORT_REGULAR));

答案 1 :(得分:1)

首先使用$array2重新索引idarray_column可以轻松实现这一目标:

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

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

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

答案 2 :(得分:0)

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

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