Rakesh Jesadiya
Rakesh Jesadiya

Reputation: 448

How to merge subarray values and generate a 1-dimensional array of unique values?

How to get final unique array result from multiple array?

I have an array like this:

Array
    (
    [0] => Array
        (
            [0] => 8
            [1] => 9
            [2] => 7
        )

    [1] => Array
        (
            [0] => 7
            [1] => 8
            [2] => 9
            [3] => 33
            [4] => 21
        )

    [2] => Array
        (
            [0] => 11
            [1] => 12
            [2] => 33
            [3] => 21
            [4] => 9
            [5] => 31
        )
  )

Expected result:

Array(
    [0] => 7
    [1] => 8
    [2] => 9
    [3] => 33
    [4] => 21
    [5] => 11
    [6] => 12
    [7] => 31
)

How to do that using php?

Upvotes: 1

Views: 1131

Answers (4)

mickmackusa
mickmackusa

Reputation: 48031

Method #1: foreach loops with isset() that sort values by their first occurrence (Demo)
(*this method seems to be the fastest of all)

$array=[[8,9,7],[7,8,9,33,21],[11,12,33,21,9,31]];
foreach($array as $sub){
    foreach($sub as $v){
        if(!isset($result[$v])){  // only add first occurence of a value
            $result[$v]=$v;
        }
    }
}
var_export(array_values($result));  // re-index and print to screen
// condensed output: array(8,9,7,33,21,11,12,31) 

Method #2: assign temporary keys which force value-overwriting to ensure no duplicates (Demo)

$array=[[8,9,7],[7,8,9,33,21],[11,12,33,21,9,31]];
foreach($array as $sub){
    foreach($sub as $v){
        $result[$v]=$v;  // force overwrite because duplicate keys cannot occur
    }
}
sort($result);  // sort and re-index
var_export($result);  // print to screen
// condensed output: array(7,8,9,11,12,21,31,33) 

Method #3: array_merge() with splat operator and array_unique() (Demo)

$array=[[8,9,7],[7,8,9,33,21],[11,12,33,21,9,31]];
$unique=array_unique(array_merge(...$array));  // merge all subarrays
sort($unique);  // sort and re-index
var_export($unique);  // print to screen
// condensed output: array(7,8,9,11,12,21,31,33) 

Method #4: unorthodox json_encode() & preg_match_all() (Demo) (Pattern Demo)

$array=[[8,9,7],[7,8,9,33,21],[11,12,33,21,9,31]];
$unique=preg_match_all('~\b(\d+)\b(?!.*\b\1\b)~',json_encode($array),$out)?$out[0]:[];
sort($unique);  // sort and re-index
var_export($unique);  // print to screen
// condensed output: array(7,8,9,11,12,21,31,33)

Upvotes: 0

Neil
Neil

Reputation: 14321

This takes three core PHP functions, sort, array_merg, and array_unique:

sort - sorts an array sent in by reference, meaning rather than returning a variable, it changes the order of the array itself.

array_merg - when combines with call_user_func_array will dynamically combine all the arrays together, however many there are.

array_unique - make sure there is only one of each element.

<?php
$arr = [ [8,9,7], [7,8,9,33,21], [11,12,33,21,9,31] ];
$merged = array_unique(call_user_func_array('array_merge', $arr));
sort($merged);
print_r($merged);
?>

Output:

Array
(
    [0] => 7
    [1] => 8
    [2] => 9
    [3] => 11
    [4] => 12
    [5] => 21
    [6] => 31
    [7] => 33
)

And here's it inside of eval.in: https://eval.in/752793

Upvotes: 2

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72269

In your desired output indexes are same, you never achieve that. because same indexes are over-written by most recent values.

You can get like below:-

$final_array = array_unique(call_user_func_array('array_merge', $array)); //convert multi-dimensional array to single dimensional and remove duplicates
asort($final_array); // sort by value. this is optional
$final_array = array_values($final_array); // re-index final array and this is optional too
echo "<pre/>";print_r($final_array); // print final array

Output:- https://eval.in/752750

Upvotes: 2

Niklesh Raut
Niklesh Raut

Reputation: 34924

This the way

<?php
    $arr = [ [8,9,7], [7,8,9,33,21], [11,12,33,21,9,31] ];
    $final = array();    
    foreach($arr as $child){
      foreach($child as $value){
        $final[] = $value;
      }
    }
    $final = array_unique($final);
    print_r($final);
?>

Demo : https://eval.in/752766

Output :

Array
(
    [0] => 8
    [1] => 9
    [2] => 7
    [6] => 33
    [7] => 21
    [8] => 11
    [9] => 12
    [13] => 31
)

Upvotes: 1

Related Questions