2

Is there any way to put two or more arrays into the array_unique() function? If not, is there any other solution to get unique results from multiple arrays?

2
  • 7
    merge arrays first, then use array_unique Commented Jun 29, 2016 at 18:17
  • Possible duplicate stackoverflow.com/questions/5211900/… Commented Jun 29, 2016 at 18:20

2 Answers 2

9

The answer to the first part of your question is NO.

The array_unique function definition in the PHP manual states that array_unique takes exactly two arguments, one array, and an optional integer that determines the sorting behavior of the function.

array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )

Rather than take the manual's word for it, here are some test arrays.

$one_array      = ['thing', 'another_thing', 'same_thing', 'same_thing'];
$two_arrays     = ['A', 'B', 'C', 'thing', 'same_thing'];
$or_more_arrays = ['same_thing', 1, 2, 3];

A couple of test show that the function does work as advertised:

$try_it = array_unique($one_array);

returns ['thing', 'another_thing', 'same_thing'];

$try_it = array_unique($one_array, $two_arrays);

gives you a warning

Warning: array_unique() expects parameter 2 to be integer, array given

and returns null.

$try_it = array_unique($one_array, $two_arrays, $or_more_arrays);

also gives you a warning

Warning: array_unique() expects at most 2 parameters, 3 given

and returns null.


The answer to the second part of your question is YES.

To get unique values using array_unique, you do have to have one array of values. You can do this, as u_mulder commented, by using array_merge to combine the various input arrays into one before using array_unique.

$unique = array_unique(array_merge($one_array, $two_arrays, $or_more_arrays));

returns

['thing', 'another_thing', 'same_thing', 'A', 'B', 'C', 1, 2, 3];

If instead of several individual array variables, you have an array of arrays like this:

$multi_array_example = [
    [1, 2, 3],
    [2, 3, 4], 
    [3, 4, 5],
    [4, 5, 6]
];

Then you can unpack the outer array into array merge to flatten it before using array_unique.

$unique = array_unique(array_merge(...$multi_array_example));

Or in older PHP versions (<5.6) before argument unpacking, you can use array_reduce with array_merge.

$unique = array_unique(array_reduce($multi_array_example, 'array_merge', []));

returns [1, 2, 3, 4, 5, 6]

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

3 Comments

Thank you " Don't Panic " for your awesome grate complete answer I wish everyone answers like you.Thank you so much.
This answer is so satisfying and elegantly presented. Dude. Thank you.
@CosetteN It's very nice of you to say so. I appreciate it!
0

By default, array_unique() takes two parameters, the array, of type array takes the input array and the second parameter, sorting of type int takes the sorting behavior. You can read more on it in the PHP manual for array_unique().

But as suggested by @u_mulder, you merge the array first, probably into a temporary array (you can learn how to do that here) and then apply the array_unique to that temporary array.

Comments

Your Answer

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