Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two multidimensional arrays then create array of only unique

I've been trying to get this for hours now.

I have two multidimensional arrays

    $newData (
    [0] => Array(
        [id] => 1
        [name] => John
        [sex] => male
    )
    [1] => Array(
        [id] => 2
        [name] => Kenny
        [sex] => male
    )
    [2] => Array(
        [id] => 3
        [name] => Sarah
        [sex] => female
    )
    [3] => Array(
        [id] => 4
        [name] => George
        [sex] => male
    )
)

$oldData (
    [0] => Array(
        [id] => 3
        [name] => Sarah
        [sex] => female
    )
    [1] => Array(
        [id] => 4
        [name] => George
        [sex] => male
    )
    [2] => Array(
        [id] => 5
        [name] => Peter
        [sex] => male
    )
    [3] => Array(
        [id] => 6
        [name] => Lexi
        [sex] => female
    )
)

I need to compare $newData and $oldData and grab only the new data that is before the first common array.

My $newData will then be:

$newData (
[0] => Array(
    [id] => 1
    [name] => John
    [sex] => male
)
[1] => Array(
    [id] => 2
    [name] => Kenny
    [sex] => male
)

)

I've tried everything from array_unique, if comparing the ID keys but nothing is working properly.

Do I need to merge them, first? map them? Bahh, I have no idea, I am so lost.

Any help would be awesome

like image 854
Ryan Avatar asked Apr 21 '13 19:04

Ryan


People also ask

How to compare two dimensional arrays in Java?

In short, to compare two dimensional arrays we have implemented a method as described below: The example’s method is boolean equal (final int [] [] arr1, final int [] [] arr2). The method takes as parameters two int arrays, and returns a boolean, that is true if the arrays are equal and false otherwise.

How to resolve or get unique value from the filtered array?

To resolve or get unique value from the filtered array, we added the filter () method again and using the indexOf () to compare values from self and fetch unique values. Note: Old Browsers (<ie9),do not support the native methods filter and includes .For those you can go for the next method.

How to check if two arrays of iNTS are equal?

The method returns true if the two specified arrays of ints are equal to one another. Two arrays are considered equal if they contain the same elements in the same order. Also, two array references are considered equal if both are null. Create two int arrays, with two dimensions and invoke the example’s method in order to check if they are equal.

How do you compare two arrays in a flow?

The most common method for comparing these two arrays will involve using an apply to each, maybe even nested apply to each actions. This results in a very inefficient flow. It will use up far more api calls than are required and result in your flow taking a lot longer to run.


1 Answers

I would just do a nested foreach loop. I don't know which programming language You're using, but assuming that it's PHP ($):

$tmpArray = array();

foreach($newData as $data1) {

  $duplicate = false;
  foreach($oldData as $data2) {
    if($data1['id'] === $data2['id'] && $data1['name'] === $data2['name'] && $data1['sex'] === $data2['sex']) $duplicate = true;
  }

  if($duplicate === false) $tmpArray[] = $data1;
}

You then have the desired array in the $tmpArray variable. You can make of course $newData = $tmpArray; afterwards.

like image 111
Maxim Zubarev Avatar answered Nov 07 '22 12:11

Maxim Zubarev