AlexB AlexB - 4 months ago 37
PHP Question

Merge array to output an array with only unique values

This question may have already been answered so sorry for duplicate if such does exist.

I have 2 arrays that I need merged to create another array with unique values.

Array 1

Array
(
[0] => Array
(
[id] => 8
[file_extension_id] => 8
[extension] =>
[name] => GS
[file_version] => 1.0.2
)

[1] => Array
(
[id] => 2
[file_extension_id] => 2
[extension] => gif
[name] => GIF
[file_version] => 1.0.2
)

[2] => Array
(
[id] => 1
[file_extension_id] => 1
[extension] => png
[name] => PNG
[file_version] => 1.0.2
)

[3] => Array
(
[id] => 19
[file_extension_id] => 19
[extension] => jpg
[name] => JPG
[file_version] => 1.0.2
)

[4] => Array
(
[id] => 20
[file_extension_id] => 20
[extension] => pdf
[name] => PDF
[file_version] => 1.0.2
)

)


Array 2

Array
(
[1] => Array
(
[id] => 1
[file_extension_id] =>
[extension] => png
[name] => PNG
[file_version] =>
)

[2] => Array
(
[id] => 2
[file_extension_id] =>
[extension] => gif
[name] => GIF
[file_version] =>
)

[3] => Array
(
[id] => 3
[file_extension_id] =>
[extension] => doc
[name] => Word 2003
[file_version] =>
)

[4] => Array
(
[id] => 4
[file_extension_id] =>
[extension] => docx
[name] => Word 2007+
[file_version] =>
)

[5] => Array
(
[id] => 5
[file_extension_id] =>
[extension] => xlsx
[name] => Excel 2010+
[file_version] =>
)

[6] => Array
(
[id] => 6
[file_extension_id] =>
[extension] => docx
[name] => Word 2010+
[file_version] =>
)

[7] => Array
(
[id] => 7
[file_extension_id] =>
[extension] => numbers
[name] => Mac Numbers
[file_version] =>
)

[8] => Array
(
[id] => 8
[file_extension_id] =>
[extension] =>
[name] => GS
[file_version] =>
)

[9] => Array
(
[id] => 9
[file_extension_id] =>
[extension] =>
[name] => Google Docs
[file_version] =>
)

[10] => Array
(
[id] => 10
[file_extension_id] =>
[extension] => ots
[name] => OpenOffice.org Calc
[file_version] =>
)

[11] => Array
(
[id] => 11
[file_extension_id] =>
[extension] => ott
[name] => OpenOffice.org Writer
[file_version] =>
)

[12] => Array
(
[id] => 12
[file_extension_id] =>
[extension] => xlsx
[name] => Excel for iPad
[file_version] =>
)

[13] => Array
(
[id] => 13
[file_extension_id] =>
[extension] => xlsm
[name] => Excel 2007 Macros
[file_version] =>
)

[14] => Array
(
[id] => 14
[file_extension_id] =>
[extension] => xlsm
[name] => Excel 2010 Macros
[file_version] =>
)

[15] => Array
(
[id] => 15
[file_extension_id] =>
[extension] => xlsx
[name] => Excel 2013+
[file_version] =>
)

[16] => Array
(
[id] => 16
[file_extension_id] =>
[extension] => xlsm
[name] => Excel 2013 Macros
[file_version] =>
)

[17] => Array
(
[id] => 17
[file_extension_id] =>
[extension] => xlsx
[name] => Excel 2016
[file_version] =>
)

[18] => Array
(
[id] => 18
[file_extension_id] =>
[extension] => xlsm
[name] => Excel 2016 Macros
[file_version] =>
)

[19] => Array
(
[id] => 19
[file_extension_id] =>
[extension] => jpg
[name] => JPG
[file_version] =>
)

[20] => Array
(
[id] => 20
[file_extension_id] =>
[extension] => pdf
[name] => PDF
[file_version] =>
)

[21] => Array
(
[id] => 21
[file_extension_id] =>
[extension] => xlsx
[name] => Excel for iPhone
[file_version] =>
)

[22] => Array
(
[id] => 22
[file_extension_id] =>
[extension] => xlsx
[name] => Excel for Android
[file_version] =>
)

[23] => Array
(
[id] => 23
[file_extension_id] =>
[extension] => xls
[name] => Excel 2003 Macros
[file_version] =>
)

[24] => Array
(
[id] => 24
[file_extension_id] =>
[extension] => docx
[name] => Word 2013+
[file_version] =>
)

[25] => 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()


Any help is appreciated

Answer

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;
}
Comments