How to create an array of combinations that contains the maximum length?

I have these three arrays:

$arr1 = ['one', 'two', 'three']; $arr2 = ['three', 'four']; $arr3 = ['two', 'five', 'six', 'seven']; 

And this is the expected result:

 /* Array ( [0] => one [1] => two [3] => three [4] => four [5] => five [6] => six [7] => seven ) 

Here is my solution that does not work properly:

 print_r( array_unique( $arr1 + $arr2 + $arr3) ); /* Array ( [0] => one [1] => two [2] => three [3] => seven ) 

How can i do this?

+5
source share
5 answers

Use this:

 array_unique(array_merge($arr1,$arr2,$arr3), SORT_REGULAR); 

this will combine the arrays into one, then remove all duplicates

Tested Here

It outputs:

 Array ( [0] => one [1] => two [2] => three [4] => four [6] => five [7] => six [8] => seven ) 
+9
source

I think it will work great

 $arr1 = ['one', 'two', 'three']; $arr2 = ['three', 'four']; $arr3 = ['two', 'five', 'six', 'seven']; $n_array = array_values(array_unique(array_merge($arr1 , $arr2 , $arr3))); echo "<pre>";print_r($n_array);echo "</pre>";die; 

Output

 Array ( [0] => one [1] => two [2] => three [3] => four [4] => five [5] => six [6] => seven ) 
+3
source

Just do it .. use array_uniqe

Demo: https://eval.in/827705

 <?php $arr1 = ['one', 'two', 'three']; $arr2 = ['three', 'four']; $arr3 = ['two', 'five', 'six', 'seven']; print_r ($difference = array_unique(array_merge($arr1, $arr2,$arr3))); ?> 
+1
source

use array_merge then array_unique

 $arr1 = ['one', 'two', 'three']; $arr2 = ['three', 'four']; $arr3 = ['two', 'five', 'six', 'seven']; print_r(array_unique (array_merge($arr1,$arr2,$arr3))); 

Result

 Array ( [0] => one [1] => two [2] => three [4] => four [6] => five [7] => six [8] => seven ) 
0
source
 <?php $arr1 = ['one', 'two', 'three']; $arr2 = ['three', 'four']; $arr3 = ['two', 'five', 'six', 'seven']; $merge_arr = array_merge($arr1,$arr2,$arr3); $unique_arr = array_unique($merge_arr); echo '<pre>'; print_r($unique_arr); echo '</pre>'; ?> 

Output

 Array ( [0] => one [1] => two [2] => three [4] => four [6] => five [7] => six [8] => seven ) 
0
source

Source: https://habr.com/ru/post/1269503/


All Articles