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
5
votes
5 answers

Filter 2D associative array using keys from multiple levels of another 2D associative array

I have two 2-dimensional arrays and want to filter the first array's data using the second array so that the only elements retained are where the keys in the first and second levels match. $array1 = [ 'a1' => ['a_name' => 'aaaaa', 'a_value' =>…
kcssm
  • 1,727
  • 3
  • 16
  • 19
4
votes
2 answers

finding the smallest array that intersects with a set of arrays

Say I have three arrays - ['a', 'b'], ['b', 'c'] and ['d']. If I were to create a fourth array that intersects with these three arrays with a minimal number of elements the array I'd get would be ['b', 'd']. My question is... how would I go about…
neubert
  • 15,947
  • 24
  • 120
  • 212
4
votes
1 answer

How can I get case-sensitive return from array_intersect()

I have two arrays and I need to compare that and return matched value from array1. Please refer my code below, $array1 = array("a" => "Green", "Red", "Blue"); $array2 = array("b" => "grEEn", "yellow", "red"); $result =…
Elavarasan
  • 2,579
  • 2
  • 25
  • 42
4
votes
2 answers

PHP array_intersect case-insensitive and ignoring tildes

Is there any function similar to "array_intersect" but it is in mode case-insensitive and ignoring tildes? The array_intersect PHP function compares array elements with === so I do not get the expected result. For example, I want this code…
Dayron Gallardo
  • 1,502
  • 2
  • 21
  • 37
4
votes
1 answer

array_intersect within foreach loop

this is my first time posting here, although I have gotten many great tips and techniques reading the posts here. Here is my objective: I have 2 somewhat similar tables to compare. For each row of each table, I am pulling the fields I want into…
Shawn Abramson
  • 701
  • 1
  • 5
  • 18
4
votes
4 answers

array_intersect a variable amount of arrays

I am creating a faceted search, and I'm am trying to use array_intersect to compare the arrays and find the inputs that match. The problem is that I will have a variable amount of arrays at anytime depending on what filters the user has…
geoctrl
  • 685
  • 7
  • 22
3
votes
1 answer

How to print similar value of arrays?

I've got an array of arrays that may have a different value and maybe the same values. i want to print common values of array ... thanks for your helps Process of array : foreach($array[0]['#items'] as $newmyarray) { …
Bijan Zand
  • 415
  • 4
  • 14
3
votes
4 answers

How can I get the duplicate multidimensional array in php

I have an multidimensional array: Array ( [0] => Array ( [a] => 1 [b] => 2 [c] => 3 [d] => 4 ) [1] => Array ( [a] => 1 [b] => 5 …
3
votes
2 answers

PHP array_intersect + array_flip with array that has values multiple times

I have two arrays: $arr1 = array(101 => 250, 102 => 250, 103 => 250, 104 => 500, 105 => 500, 106 => 500,); and $arr2 = array(0 => 103, 1 => 104, 2 => 105) The result I want to get is Array (103 => 250, 104 => 500) I have tried working with…
Tris
  • 151
  • 1
  • 10
3
votes
2 answers

How can I get the key intersect of two arrays?

I have two arrays as shown blow //array 1 Array ( [0] => 223 [1] => 216 ) /array 2 Array ( [221] => Bakers [220] => Construction [223] => Information Technology [216] => Jewellery [217] => Photography [222] =>…
Manish Prajapati
  • 1,583
  • 2
  • 13
  • 21
3
votes
1 answer

php - "first 2 words" multi-match for values in array then array_intersect?

first let me apologize, I'm a Network engineer and not a coder... so please if you will bear with me here. Here's what I'm up against and I can't for the life of me find an elegant way to do it. I'm using nagios (sure many of you are familiar with…
Ben Shellrude
  • 79
  • 1
  • 7
3
votes
2 answers

PHP Array_intersect on multidimensional array with unknown number of keys

I'm trying to make advanced search filters in an application that holds resources (people). I've got all the results in 1 multidimensional array. A user of the application can search for the persons Job title, skills, work field and country. I've…
Mettin Parzinski
  • 838
  • 1
  • 7
  • 13
3
votes
3 answers

Filter a flat, associative array by the keys in another flat, associative array

I have two arrays: $arr1 = array('a' => 10, 'b' => 20); $arr2 = array('a' => 10, 'b' => 20, 'c' => 30); How can I use array_filter() to drop elements from $arr2 that don't exist in $arr1? Like "c" in my example.
Alex
  • 66,732
  • 177
  • 439
  • 641
3
votes
1 answer

How can I preserve multidimensional array information when performing array intersections in PHP?

I have many arrays containing an ID as the primary key, with multidimensional information under each id. Here are two examples: First Example Array: array 14181 => array 'industries' => array 'weight' => string…
T. Brian Jones
  • 13,002
  • 25
  • 78
  • 117
2
votes
1 answer

How to count the number of elements in one array (with duplicates) that match with elements of another array in SQL(Presto)?

I have two arrays X,Y. X=[a,b,c] and Y=[a,a,b,b,b,c,d,d,e,e,e]. I want to write a query that will return the number of elements in Y that match the elements in X (with duplicates). in this case the out put should be [a,a,b,b,b,c] and I need the…
1
2
3
11 12