9
$arr[] = array('A','B');
$arr[] = array('C','B');
...

I need to get the merged result of all sub array of $arr .

And for duplicated entries,should fetch only one.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
user198729
  • 61,774
  • 108
  • 250
  • 348

7 Answers7

25

If you really don't want to loop, try this:

$arr[] = array('A','B');
$arr[] = array('C','B');
$arr[] = array('C','D');
$arr[] = array('F','A');
$merged = array_unique(call_user_func_array('array_merge', $arr));
bish
  • 871
  • 8
  • 9
  • I think there is no need for `array_unique` as `array_merge` will overwrite duplicate keys if found any. Can anybody please reply me? – Ankit Apr 02 '16 at 05:17
  • without the 'array_merge' the following error comes up "ErrorException: call_user_func_array() expects exactly 2 parameters" @Ankit – LeRoy Apr 26 '16 at 11:28
  • genius, one of the rare cases when we can actually use call_user_func_array and it's like a perfect fit for this kind of thing. – Robert Sinclair Aug 16 '17 at 00:22
15

OK through another question I found out that the following is actually possible (I tried myself without success, I had the wrong version) if you use PHP version >= 5.3.0:

$merged_array = array_reduce($arr, 'array_merge', array());

If you only want unique values you can apply array_unique:

$unique_merged_array = array_unique($merged_array);

This works if you only have flat elements in the arrays (i.e. no other arrays). From the documentation:

Note: Note that array_unique() is not intended to work on multi dimensional arrays.

If you have arrays in your arrays then you have to check manually e.g. with array_walk:

$unique_values = array();

function unique_value($value, &$target_array) {
    if(!in_array($value, $target_array, true))
        $target_array[] = $value;
}

array_walk($merged_values, 'unique_value', $unique_values);

I think one can assume that the internal loops (i.e. what array_walk is doing) are at least not slower than explicit loops.

jave.web
  • 13,880
  • 12
  • 91
  • 125
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
8
array_unique(array_merge($arr[0], $arr[1]));

or for an unlimited case, I think this should work:

$result_arr = array();
foreach ($arr as $sub_arr) $result_arr = array_merge($result_arr, $sub_arr);
$result_arr = array_unique($result_arr);
sprugman
  • 19,351
  • 35
  • 110
  • 163
1

This answer is based on Felix Kling post. It's more accurate version with fixing "non array" sub-elements.

$res = array_reduce($res, function(&$res, $v) {
                            return array_merge($res, (array) $v);
                        }, array());
23W
  • 1,413
  • 18
  • 37
  • 1
    Great solution and use array_filter() to get rid of empty array elements caused by empty array values or strings added previously. Here is the final version: $res = array_filter(array_reduce($res, function(&$res, $v) {return array_merge($res, (array) $v);}, array())); – Tarik Oct 23 '16 at 21:42
1

For PHP 5.6 with splat operator:

array_unique(array_merge(...$arr));
Gannet
  • 1,315
  • 13
  • 18
1

A simple way to handle this in PHP Version >= 5.6 is to use the splat operator also called Argument Unpacking.

$arr = array_unique(array_merge(...$arr));
wnull
  • 217
  • 6
  • 21
Mat Lipe
  • 725
  • 8
  • 14
1

$merged_array = array_reduce($serp_res, 'array_merge', array()); with added quotas,'array_merge', worked for me.

Goce Ribeski
  • 1,352
  • 13
  • 30