I am aware that I can use array_unique(array_merge($a,$b));
to merge two arrays and then remove any duplicates,
but,
is there an individual function that will do this for me?
(I know I could write one myself that just calls these, but I am just wondering).
There is no such function. Programming languages in general give you a certain set of tools (functions) and you can then combine them to get the results you want.
There really is no point in creating a new function for every use case there is, unless it is a very common use case - and yours does not seem to be one.
No, array_unique(array_merge($a,$b));
is the way to do it.
See the list of array functions.
By default no there isn't. You can do it another way (which might not be that smart though)
$array1 = array('abc', 'def');
$array2 = array('zxd', 'asdf');
$newarray = array_unique(array($array1, $array2));
does sort of the same thing. Just wondering why isn't what you posted good enough?
//usage $arr1=array(0=>"value0", 1=>"value1", 2=>"value2"); $arr2=array(0=>"value3", 1=>"value4", 2=>"value0") => $result=mergeArrays($arr1,$arr2); $result=Array ( [0] => value0 [1] => value1 [2] => value2 [3] => value3 [4] => value4 )
function mergeArrays () {
$result=array();
$params=func_get_args();
if ($params) foreach ($params as $param) {
foreach ($param as $v) $result[]=$v;
}
$result=array_unique($result);
return $result;
}
In php 5.6+ you can also use the new argument unpacking to avoid multiple array_merge calls (which significantly speed up your code): https://3v4l.org/arFvf
<?php
$array = [
[1 => 'french', 2 => 'dutch'],
[1 => 'french', 3 => 'english'],
[1 => 'dutch'],
[4 => 'swedish'],
];
var_dump(array_unique(array_merge(...$array)));
Outputs:
array(4) {
[0]=>
string(6) "french"
[1]=>
string(5) "dutch"
[3]=>
string(7) "english"
[5]=>
string(7) "swedish"
}