如何制作包含最大长度的组合数组?

时间:2017-07-05 04:29:37

标签: php arrays function

我有这三个数组:

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

这是预期的结果:

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

Here是我的解决方案,无法正常工作:

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

我该怎么做?

5 个答案:

答案 0 :(得分:9)

使用此:

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

这会将数组合并为一个,然后删除所有重复项

经过测试Here

输出:

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

答案 1 :(得分:3)

我认为这样可以正常使用

$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;

输出

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

答案 2 :(得分:1)

  

就这样做..使用array_uniqe

     

演示: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)));
    ?>

答案 3 :(得分:0)

然后使用array_merge,array_unique

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

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

结果

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

答案 4 :(得分:0)

<?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>';
?>

输出

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