5

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).

2
  • Cheers guys, I was simply curious to find out if there was a php function so I would not have to call another function on the array (it can be quite large). Commented Nov 4, 2010 at 21:56
  • 1
    If you do just write a function anyway so you don't keep writing array_unique(array_merge(...)), keep in mind that array_merge() can accept more than 2 arguments. Just a thought :) Commented Nov 4, 2010 at 21:58

5 Answers 5

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

There are plenty of array functions, another one doesn't count... Thinking of arrays as sets and merge as union, well, that's common.
PHP is precisely one of the languages that has lots and lots of auxiliary methods that are not really completely needed but still are really handy. More so than any other language I know.
5

No, array_unique(array_merge($a,$b)); is the way to do it.

See the list of array functions.

Comments

0

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?

5 Comments

array(array(/* some data */), array(/* some other data */)) is not the same as array_merge() at all: array merge() combines all values of the arrays into one array; array() simply combines the arrays themselves as values into one array. By proxy, array_unique will not do what the OP is trying to achieve.
Just that your answer is not what the OP is trying to achieve (and does not do 'sort of the same thing', either), thus not an answer to their problem. Making multi-dimensional arrays with array() might be insightful to someone in general (<-- I'm serious), but not in the scope of this question. Seeing as this place is a bit like a Q&A encyclopaedia, it's worth pointing out that your answer doesn't fit.
@pinkgothic Why are you posting it 2 month later? It's obviously not what the OP is looking for otherwise it'd of been flagged up before etc... No need to come 2 months later tagging a number of posts and throwing on comments pretending to be active.
Well, I'm an active user of the site, as in, read it a lot. If I find something that requires a comment, I add it. :) That way other people who come across the old question are informed. (That's kind of the point of stackoverflow.)
7 years later, I'm here to verify that @pink
0
//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;
}

Comments

0

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"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.