Questions tagged [array-difference]

Used for questions concerning the difference between arrays. For example, where elements in two arrays differ from each other, or subtracting the content of one array from another. Since the tag can be broadly applied to any programming language that supports arrays, use use one or more additional tags in order to help identify the specific language the question relates.

An array is a data structure that encompasses a group of elements. When working with arrays there can be a requirement to determine the difference between two or more arrays, and this tag is for questions relating specifically to finding that difference.

The difference between arrays can be determined in a number of ways, including finding where elements within the array differ, and can include operations such as subtracting the contents of one array from another to produce an outcome.

273 questions
4
votes
5 answers

preg_replace with pattern and subject array returning empty array

I want a simple way to delete elements of $badwords from $keywords. What I have (as an example) $keywords = array('sebastian','nous','helene','lol'); //it could be anything in fact $badwords = array('nous', 'lol', 'ene', 'seba'); //array full of…
Wistar
  • 3,770
  • 4
  • 45
  • 70
3
votes
3 answers

Efficient algorithm for difference of array and a known subsequence?

I'm passing an array to a library function which returns an array which is a subsequence of the input array. That is to say the orders of the first and second array are identical but the second array may be lacking any number of elements of the…
3
votes
2 answers

How to compare 2 Ordered Dictionaries and create a new Ordered one with differences? (Python 3.7)

I'm struggling on how to generate a "Differences" Ordered-Dictionary containing the Different and also New values appearing on a "Modified" Ordered-Dictionary after comparing with a "Reference" Ordered-Dictionary. EXAMPLE: #python from collections…
3
votes
6 answers

Matching values of an array against a string PHP

I am working on a small project and I need some help. I have a CSV file with 150,000 rows (each row has 10 cols of data). I am using the fscvread to read the file and during the loop I want to match one of the columns (call it stringx) of each row…
Rohit Chopra
  • 2,791
  • 4
  • 28
  • 33
3
votes
1 answer

array_diff on array of objects

I have got a problem with PHP function array_diff. In both cases I'm using it on arrays of the same class objects. First case: public function findFreeUsers($weekId) { $em = $this->getEntityManager(); $week =…
3
votes
4 answers

How can I compare the rows in two 2d arrays?

It seems that every PHP function I read about for comparing arrays (array_diff(), array_intersect(), etc) compares for the existence of array elements. Given two multidimensional arrays with identical structure, how would you list the differences in…
Nathan Long
  • 122,748
  • 97
  • 336
  • 451
3
votes
4 answers

Retain rows in a 2d array which are not found in another 2d array

I've got two two-dimensional arrays and I need to filter the first array's rows by the second array's rows. $array1 = [ ["module1", "path/to/file.txt", ""], ["module2", "path/to/file2.txt", ""], ]; $array2 = [ ["module1",…
Eldros
  • 551
  • 1
  • 9
  • 28
3
votes
2 answers

array_udiff seems not work

I'm trying to compare two array with array_udiff, but it's very weired. it seems array_udiff not get the right answer. Here is the live demo. The result should be an empty array, but leave one element unfiltered out.
LF00
  • 27,015
  • 29
  • 156
  • 295
3
votes
1 answer

Filter array based on second array

I have an array $data and it is required to be filtered based on another array $clr. I have done it by foreach and solved my purpose but I am looking for an optimum way like map or filter. What I have tried is: $clr = [1, 2, 4, 6, 8, 13, 21]; $data…
Janie
  • 638
  • 9
  • 26
3
votes
3 answers

How to compare two arrays that contain user details as array and remove the matching elements from one array?

I've two arrays $a and $b containing 'user details' in arrays. Following is the structure of it(i.e. output of print_r()) print_r($a); Array ( [0] => Array ( [user_id] => 109 [profile_page_id] => 0 …
PHPLover
  • 1
  • 51
  • 158
  • 311
3
votes
2 answers

Removed empty array element to json_encode after

I have simple array $a = ['a', '', 'b','c','d']; if i json_encode it i ll have ["a","","b","c","d"] But if i ll try to remove empty value, with array_filter or array_diff i ll have {"0":"a","2":"b","3":"c","4":"d"} but i dont need array keys to…
Itsmeromka
  • 3,621
  • 9
  • 46
  • 79
3
votes
2 answers

Sorting Multidimensional Arrays With Uasort

Currently I have some multidimensional arrays that look like this Array ( [71] => Array ( [author] => 2 [date] => 1392867376 ) [49] => Array ( [author] => 2 [date] => 1392868188 ) [75] => Array ( [author] => 14 [date] => 1392867388) …
Noodle Head
  • 431
  • 3
  • 10
  • 23
3
votes
2 answers

PHP Multi-Array Difference

I have arrays containing objects that look like the following, first array: Array ( [1] => stdClass Object ( [matchID] => 1 [tm] => 2014-01-16 08:55:13 [playertm] => 2014-01-16 08:55:14 ) [2] => stdClass Object …
Thomas
  • 53
  • 5
3
votes
1 answer

Sum of all elements of an array after N iterations

Given an Array arr[ ]={4,6,8,3,6} sum of all elements of the array = 27. Now, let us perform an operation on the array:- For all i < length(arr)-1, arr[i]=arr[i]-arr[i+1] So now the array becomes {-2,-2,5,-3}, sum of all elements of the array =…
tintin
  • 151
  • 1
  • 2
  • 13
2
votes
1 answer

Array Difference Helpers on Laravel 9

I want to get the difference between the two arrays in my controller. If I use array_diff() function, it gives me an error array_diff(): Argument #1 ($array) must be of type array, Illuminate\Support\Collection given. $newListOfArray =…
iAmBorgy
  • 73
  • 9
1 2
3
18 19