You are viewing an older version of the site. Click here to view
the latest version of this page. (This may be a dead link, if so, try the root page of the docs
here.)
== array_intersect ==
array1, array2, comparisonClosure |- ! scope="row" | Throws | [[../objects/ms.lang.CastException|ms.lang.CastException]]
[[../objects/ms.lang.IllegalArgumentException|ms.lang.IllegalArgumentException]] |- ! scope="row" | Since | 3.3.3 |- ! scope="row" | Restricted |
Copy Code
The output would be:
Copy Code
The output would be:
Copy Code
The output would be:
Copy Code
The output would be:
Copy Code
The output would be:
Copy Code
The output would be:
Copy Code
The output would be:
Copy Code
The output would be:
Returns an array that is the intersection of the two provided arrays. If either array is associative, it puts the function in associative mode. For normal arrays, the values are compared, and for associative arrays, the keys are compared, but the values are taken from the left array. comparisonMode is only applicable for normal arrays, and defaults to HASH, but determines the mode in which the system decides if two values are equal or not. A closure may be sent instead, which should return true if the two values are considered equals or not. Using the HASH mode is fastest, as this puts the function in an optimizing mode, and it can run at O(n log n). Otherwise, the runtime is O(n**2). The results between HASH and STRICT_EQUALS should almost never be different, and so in that case using STRICT_EQUALS has a lower performance for no gain, but there may be some cases where using the hash code is not desirable. EQUALS is necessary if you wish to disregard typing, so that array(1, 2, 3) and array('1', '2', '3') are considered equal. Duplicate values in the left array are duplicated, but duplicates in the right are not.
=== Vital Info ===
{| style="width: 40%;" cellspacing="1" cellpadding="1" border="1" class="wikitable"
|-
! scope="col" width="20%" |
! scope="col" width="80%" |
|-
! scope="row" | Name
| array_intersect
|-
! scope="row" | Returns
| array
|-
! scope="row" | Usages
| array1, array2, [comparisonMode]array1, array2, comparisonClosure |- ! scope="row" | Throws | [[../objects/ms.lang.CastException|ms.lang.CastException]]
[[../objects/ms.lang.IllegalArgumentException|ms.lang.IllegalArgumentException]] |- ! scope="row" | Since | 3.3.3 |- ! scope="row" | Restricted |
No
|-
! scope="row" | Optimizations
| None
|}
=== Usages ===
array_intersect(array1, array2, [comparisonMode])
array_intersect(array1, array2, comparisonClosure)=== Examples === ====Example 1==== Usage with associative array Given the following code:
array_intersect(array(one: 1, five: 5), array(one: 1, three: 3))

1 {{function|array_intersect}}({{function|array}}(one: 1, five: 5), {{function|array}}(one: 1, three: 3))
:{one: 1}====Example 2==== Usage with normal arrays. The default comparison method is HASH Given the following code:
array_intersect(array(1, 2, 3), array(2, 3, 4))

1 {{function|array_intersect}}({{function|array}}(1, 2, 3), {{function|array}}(2, 3, 4))
:{2, 3}====Example 3==== Demonstrates that STRICT_EQUALS does not consider different types to be equal Given the following code:
array_intersect(array('1', '2', '3'), array(1, 2, 3), 'STRICT_EQUALS')

1 {{function|array_intersect}}({{function|array}}('1', '2', '3'), {{function|array}}(1, 2, 3), 'STRICT_EQUALS')
:{}====Example 4==== Note that the results of this method are the same as the previous example, but this version would be faster, and is preferred in all but the most exceptional cases. Given the following code:
array_intersect(array('1', '2', '3'), array(1, 2, 3), 'HASH')

1 {{function|array_intersect}}({{function|array}}('1', '2', '3'), {{function|array}}(1, 2, 3), 'HASH')
:{}====Example 5==== Demonstrates usage with equals. Note that '1' == 1 (but does not === 1) but since the comparison method uses equals, not sequals, these arrays are considered equivalent. Given the following code:
array_intersect(array('1', '2', '3'), array(1, 2, 3), 'EQUALS')

1 {{function|array_intersect}}({{function|array}}('1', '2', '3'), {{function|array}}(1, 2, 3), 'EQUALS')
:{1, 2, 3}====Example 6==== Usage with a custom closure Given the following code:
array_intersect(
array(array(id: 1, qty: 2), array(id: 2, qty: 5)),
array(array(id: 1, qty: 2), array(id: 5, qty: 10)),
closure(@a, @b) {
return(@a['id'] == @b['id']);
})

1 {{function|array_intersect}}(
2 {{function|array}}({{function|array}}(id: 1, qty: 2), {{function|array}}(id: 2, qty: 5)),
3 {{function|array}}({{function|array}}(id: 1, qty: 2), {{function|array}}(id: 5, qty: 10)),
4 {{keyword|closure}}(@a, @b) {
5 {{function|return}}(@a['id'] == @b['id']);
6 })
:{{id: 1, qty: 2}}====Example 7==== The value is taken from the left array. This is not important for primitives, but when using arrays and a custom closure, it may make a difference. Given the following code:
array_intersect(
array(array(id: 1, pos: 'left')),
array(array(id: 1, pos: 'right')),
closure(@a, @b) {
return(@a['id'] == @b['id']);
})

1 {{function|array_intersect}}(
2 {{function|array}}({{function|array}}(id: 1, pos: 'left')),
3 {{function|array}}({{function|array}}(id: 1, pos: 'right')),
4 {{keyword|closure}}(@a, @b) {
5 {{function|return}}(@a['id'] == @b['id']);
6 })
:{{id: 1, pos: left}}====Example 8==== Demonstrates behavior with duplicate values Given the following code:
msg(array_intersect(
array(1, 1, 1, 2, 3),
array(1, 2)));
msg(array_intersect(
array(1, 2, 3),
array(1, 1, 1)));

1 {{function|msg}}({{function|array_intersect}}(
2 {{function|array}}(1, 1, 1, 2, 3),
3 {{function|array}}(1, 2)));
4 {{function|msg}}({{function|array_intersect}}(
5 {{function|array}}(1, 2, 3),
6 {{function|array}}(1, 1, 1)));
{1, 1, 1, 2} {1}===See Also===
[[API/functions/array_merge.html|array_merge]]
, [[API/functions/array_subtract.html|array_subtract]]
Find a bug in this page? Edit this page yourself, then submit a pull request. (Note this page is automatically generated from the documentation in the source code.)