How to merge subarray values and remove duplicates?
Asked Answered
M

7

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.

Majestic answered 26/1, 2010 at 15:52 Comment(0)
M
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));
Madancy answered 26/1, 2010 at 19:49 Comment(3)
I think there is no need for array_unique as array_merge will overwrite duplicate keys if found any. Can anybody please reply me?Gutierrez
without the 'array_merge' the following error comes up "ErrorException: call_user_func_array() expects exactly 2 parameters" @GutierrezImmolate
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.Tam
A
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.

Ascocarp answered 26/1, 2010 at 16:45 Comment(2)
I don't think $merged_array = array_merge($arr); will actually break up the sub-arrays, which I'm pretty sure is what the OP was asking for.Format
Worth noting that array_merge should be in quotes e.g. 'array_merge'Campos
F
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);
Format answered 26/1, 2010 at 15:54 Comment(9)
It can have more than 2 sub arrays.Majestic
The number of sub arrays is dynamic.Majestic
Despite the author only giving $arr 0 + 1 as an example, I think they needs a solution to accommodate for all child items of $arr.Diversify
Is there a way to do it other than loop?Majestic
why? what's wrong with a loop? I suppose you could use some kind of mapping function. Look at the array section of the php docs for other ideas. php.net/manual/en/ref.array.phpFormat
If the number of arrays are dynamic, I think loops could be the best choice. Why would you not want a loop?Saturation
Is there a array function that can handle this problem?Majestic
array_walk() is a possibility, but it's more overheadFormat
It is still not correct, it has to be $result_arr = array_merge($result_arr, $sub_arr);Ascocarp
B
1

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

Basset answered 31/1, 2012 at 13:34 Comment(0)
F
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());
Fidellas answered 27/11, 2014 at 12:53 Comment(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()));Flanagan
M
1

For PHP 5.6 with splat operator:

array_unique(array_merge(...$arr));
Marnamarne answered 12/9, 2017 at 3:5 Comment(0)
H
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));
Hoeve answered 22/11, 2017 at 15:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.