php PHP中数组的集合理论联合

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10485242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:18:08  来源:igfitidea点击:

Set Theory Union of arrays in PHP

phparrays

提问by Dmytro Zarezenko

There are 3 operations with sets in mathematics: intersection, difference and union (unification). In PHP we can do this operations with arrays:

数学中的集合有 3 种运算:交、差和并(统一)。在 PHP 中,我们可以对数组进行以下操作:

  • intersection: array_intersect
  • difference: array_diff
  • 路口: array_intersect
  • 区别: array_diff

What function is for union?

union 的作用是什么?

No duplicates can be in the result array (like array_intersectand array_diff).

结果数组中不能有重复项(如array_intersectarray_diff)。

If indexes are numeric then array_mergewill not overwrite the original value, but will be appended (PHP docs).

如果索引是数字,则array_merge不会覆盖原始值,但会附加(PHP 文档)。

回答by adrien

Try array_merge:

尝试array_merge

array_unique(array_merge($array1, $array2));

PHP Manual

PHP 手册

回答by Brian

array_unique( array_merge( ... ) )

array_unique( array_merge( ... ) )

回答by Brilliand

Adrien's answer won't necessary produce a sequentially numbered array from two sequentially numbered arrays - here are some options that will:

Adrien 的答案不需要从两个按顺序编号的数组生成一个按顺序编号的数组 - 这里有一些选项可以:

array_values(array_unique(array_merge($array1, $array2)));

(Adrien's answer with renumbering the keys afterward)

(Adrien 的回答是之后对键重新编号)

array_keys(array_flip($array1)+array_flip($array2))

(Put the values in the keys, and use the array union operator)

(将值放在键中,并使用数组联合运算符)

array_merge($array1, array_diff($array2, $array1))

(Remove the shared values from the second array before merging)

(在合并之前从第二个数组中删除共享值)

Benchmark results (for merging two arrays of length 1000 a thousand times on my system):

基准测试结果(用于在我的系统上合并两个长度为 1000 的数组一千次):

  • Unique (Adrien's version): 2.862163066864 seconds
  • Values_Unique: 3.12 seconds
  • Keys_Flip: 2.34 seconds
  • Merge_Diff: 2.64 seconds
  • 唯一(Adrien 的版本):2.862163066864 秒
  • Values_Unique:3.12 秒
  • Keys_Flip:2.34 秒
  • Merge_Diff:2.64 秒

Same test, but with the two arrays being very similar (at least 80% duplicate):

相同的测试,但两个数组非常相似(至少 80% 重复):

  • Unique (Adrien's version): 2.92 seconds
  • Values_Unique: 3.15 seconds
  • Keys_Flip: 1.84 seconds
  • Merge_Diff: 2.36 seconds
  • 独特(Adrien 的版本):2.92 秒
  • Values_Unique:3.15 秒
  • Keys_Flip:1.84 秒
  • Merge_Diff:2.36 秒

It seems using the array union operator to do the actual union is the fastest method. Note however that array_flipis only safe if the array's values are all strings or all integers; if you have to produce the union of an array of objects, I recommend the version with array_mergeand array_diff.

似乎使用数组联合运算符进行实际联合是最快的方法。但是请注意,array_flip只有当数组的值都是字符串或所有整数时,这才是安全的;如果您必须生成对象数组的并集,我建议使用array_merge和的版本array_diff

回答by Oscar Jara

Use array_uniqueand array_mergetogether.

array_uniquearray_merge一起使用。

回答by Marian Zburlea

From the code in the PHP: Array Operatorsdocumentation:

来自PHP: Array Operators文档中的代码:

<?php
$a = array("a" => "apple", "b" => "banana");
$b = array("a" => "pear", "b" => "strawberry", "c" => "cherry");

$c = $a + $b; // Union of $a and $b
echo "Union of $a and $b: \n";
var_dump($c);

$c = $b + $a; // Union of $b and $a
echo "Union of $b and $a: \n";
var_dump($c);
?>

When executed, this script will print the following:

执行时,此脚本将打印以下内容:

Union of $a and $b:
array(3) {
  ["a"]=>
  string(5) "apple"
  ["b"]=>
  string(6) "banana"
  ["c"]=>
  string(6) "cherry"
}
Union of $b and $a:
array(3) {
  ["a"]=>
  string(4) "pear"
  ["b"]=>
  string(10) "strawberry"
  ["c"]=>
  string(6) "cherry"
}

回答by RJ Anoop

use "+" operator to do so. See the link Array Operators

使用“+”运算符来做到这一点。请参阅链接数组运算符

回答by ackuser

$result = array_merge_recursive($first, $second);

can be useful when you have arrays with arrays inside.

当您有包含数组的数组时,这会很有用。

回答by Clemens Tolboom

The OP did not specify whether the union is by valueor by keyand PHP has array_mergefor merging values and +for merging the keys. The results depends on whether the array is indexed or keyed andwhich array comes first.

OP 没有指定联合是按还是按键,PHP 具有array_merge合并值和+合并键的功能。结果取决于数组是索引还是键控以及哪个数组先出现。

$a = ['a', 'b'];
$b = ['b', 'c'];

$c = ['a' => 'A',  'b' => 'B'];
??$d = ['a' => 'AA', 'c' => 'C'];

Indexed array

索引数组

See array_merge

array_merge

By value using array_merge

按值使用 array_merge

??array_merge($a, $b); // [?0 => 'a', 1 => 'b', 2 => 'b', 3 => 'c']
??array_merge($b, $a); // ?[0 => 'b', 1 => 'c', 2 => 'a', 3 => 'b']

merge by key using +operator

使用+运算符按键合并

See +operator

+运营商

??$a + $b; ?// [0 => 'a', 1 => 'b']
$b + $a; // [0 => 'b', 1 => 'c']

Keyed array

键控数组

By value using array_merge

按值使用 array_merge

?array_merge($c, $d); // ?['a' => 'AA', 'b' => 'B', 'c' => 'C']
array_merge($d, $c); // ['a' => 'A',  'c' => 'C', 'b' => 'B']

merge by key using +operator

使用+运算符按键合并

$c + $d; // [?'a' => 'A',  'b' => 'B', 'c' => 'C']
??$d + $c; // ?['a' => 'AA', 'c' => 'C', 'b' => 'B']

回答by Paul

The +operator:

+操作:

$x[0] = 4;
$x[1] = 1;

$y[0] = 9;
$y[2] = 5;

$u = $y + $x;

// Results in:
$u[0] === 9;
$u[1] === 1;
$u[2] === 5;

Note that $x + $y!= $y + $x

请注意$x + $y!=$y + $x

回答by Altaf Hussain

Just use $array1 + $array2It will result union of both array.

只需使用 $array1 + $array2它将导致两个数组的并集。