Questions tagged [array-intersect]

PHP's array_intersect() function accepts the array with master values to check, an array to compare values against and a variable list of arrays to compare. It returns an array containing all of the values in master array whose values exist in all of the parameters.

PHP's array_intersect() function accepts the array with master values to check, an array to compare values against and a variable list of arrays to compare. It returns an array containing all of the values in master array whose values exist in all of the parameters.

169 questions
84
votes
3 answers

rails - Finding intersections between multiple arrays

I am trying to find the intersection values between multiple arrays. for example code1 = [1,2,3] code2 = [2,3,4] code3 = [0,2,6] So the result would be 2 I know in PHP you can do this with array_intersect I wanted to be able to easily add…
Alex
  • 6,205
  • 7
  • 43
  • 53
65
votes
4 answers

Opposite of array_intersect?

Is there a built-in function to get all members of array 1 which do not exist in array 2? I know how to do it programatically, only wondering if there is a built-in function that does the same. So please, no code examples.
Itay Moav -Malimovka
  • 52,579
  • 61
  • 190
  • 278
31
votes
3 answers

Using array_intersect on a multi-dimensional array

I have two arrays that both look like this: Array ( [0] => Array ( [name] => STRING [value] => STRING ) [1] => Array ( [name] => STRING [value] => STRING ) …
Nathan
  • 2,699
  • 5
  • 30
  • 44
15
votes
5 answers

Intersect unknown number of arrays in PHP

I'm trying to intersect an arbitrary number of PHP arrays, the count of which depends on a user provided parameter, each of which can have any number of elements. For example: array1(1, 2, 3, 4, 5) array2(2, 4, 6, 8, 9, 23) array3(a, b, 3, c,…
Swader
  • 11,387
  • 14
  • 50
  • 84
15
votes
3 answers

PHP: How to compare keys in one array with values in another, and return matches?

I have the following two arrays: $array_one = array('colorZero'=>'black', 'colorOne'=>'red', 'colorTwo'=>'green', 'colorThree'=>'blue', 'colorFour'=>'purple', 'colorFive'=>'golden'); $array_two = array('colorOne', 'colorTwo', 'colorThree'); I want…
Solace
  • 8,612
  • 22
  • 95
  • 183
11
votes
3 answers

Intersection of two lists, keeping duplicates in the first list

I have two flat lists where one of them contains duplicate values. For example, array1 = [1,4,4,7,10,10,10,15,16,17,18,20] array2 = [4,6,7,8,9,10] I need to find values in array1 that are also in array2, KEEPING THE DUPLICATES in array1. Desired…
user32147
  • 1,033
  • 4
  • 12
  • 22
9
votes
1 answer

PHP: in_array() vs array_intersect() performance

What, and how much, is faster - manually iterating over an array with foreach and checking for needle occurrence with in_array(), or using array_intersect()?
The Onin
  • 5,068
  • 2
  • 38
  • 55
8
votes
2 answers

Check if an array contains another array with PHP

Possible Duplicate: Checking if an array contains all elements of another array I have posted something like this to the Stackoverflow before, but the answers do not fully satisfy me. That's why I'm posting the question again, but changing the…
mozcelikors
  • 2,582
  • 8
  • 43
  • 77
6
votes
5 answers

Fast count() intersection of two string arrays

I need to count the number of elements corresponding to the intersection of two big arrays of strings and do it very fast. I am using the following code: arr1[i].Intersect(arr2[j]).Count() For CPU Time, VS Profiler indicates 85.1% in…
LeMoussel
  • 5,290
  • 12
  • 69
  • 122
6
votes
6 answers

Checking if an array contains all elements of another array

I'm designing an electrical engineering application. However, i'm stuck on this: I have the following array
mozcelikors
  • 2,582
  • 8
  • 43
  • 77
5
votes
5 answers

Why does array_uintersect() compare elements between array1 & array2, array1 & array1, and array2 & array2?

Test script $i = 0; array_uintersect(['foo', 'bar'], ['baz', 'qux'], function($a, $b) use (&$i) { print_r([$a, $b, $i++]); }); Actual Result Array ( [0] => bar [1] => foo [2] => 0 ) Array ( [0] => qux [1] => baz [2] =>…
The Onin
  • 5,068
  • 2
  • 38
  • 55
5
votes
3 answers

Filter 2D array by comparing column values against a differently-keyed column in another array

I have 2 arrays that I'm working with. The first array comes from the data of a CSV file and the other is a response from an API. Is it possible to filter array 2 by using matching values from array 1? Array 1 Example [ ["B00CEEZ57S"], …
mattchambers
  • 185
  • 3
  • 15
5
votes
3 answers

How to do PHP array_intersect by keys not by values?

$master = ['111' => 'foo', '124' => 'bar', '133' => 'baz']; $check = ['111' => 14, '133' => 23 ]'; I want to remove all keys from $master that do not exists in $check. So the result in this example should be: $newMaster = ['111' => 'foo', '133' =>…
black-room-boy
  • 659
  • 2
  • 11
  • 23
5
votes
4 answers

in Python find number of same elements in 2 lists

In Python if I have 2 lists say: l1 = ['a', 'b', 'c', 'd'] l2 = ['c', 'd', 'e'] is there a way to find out how many elements they have the same. In the case about it would be 2 (c and d) I know I could just do a nested loop but is there not a built…
John
  • 21,047
  • 43
  • 114
  • 155
5
votes
2 answers

Compare array with duplicate values using array_intersect?

I'm designing a package engine for my catalog. Here you can add a certain ammount of products to the package and a discount. When you order products the script have to detect which package deals apply to your order. Here is my code: //…
1
2 3
11 12