-1

I'm working with 2 PHP arrays.

print_r($matches);
print_r($dexcode_comp);

$matches and dexcode_comp.

Their outcomes are as follow:

$matches:

{
    0: {
        simplicate_dexcode: "BeS712210",
        callmonkey_dexcode: "BeS712210"
    },
    1: {
        simplicate_dexcode: "BeS712210",
        callmonkey_dexcode: "BeS712210"
    },
    2: {
        simplicate_dexcode: "BeS712210",
        callmonkey_dexcode: "BeS712210"
    },
    3: {
        simplicate_dexcode: "S-T41471",
        callmonkey_dexcode: "S-T41471"
    },
    4: {
        simplicate_dexcode: "Ron35844",
        callmonkey_dexcode: "Ron35844"
    },
    5: {
        simplicate_dexcode: "Lin10961",
        callmonkey_dexcode: "Lin10961"
    },
    6: {
        simplicate_dexcode: "Tip295926",
        callmonkey_dexcode: "Tip295926"
    },
    7: {
        simplicate_dexcode: "Lin10961",
        callmonkey_dexcode: "Lin10961"
    }
},

$dexcode_comp:

{
    0: {
        dexcode: "BeS712210"
    },
    1: {
        dexcode: "De 48245"
    },
    2: {
        dexcode: "Bis1016338"
    },
    3: {
        dexcode: "S-T41471"
    },
    4: {
        dexcode: "Ron35844"
    },
    5: {
        dexcode: "Lin10961"
    },
    6: {
        dexcode: "Tip295926"
    },
    7: {
        dexcode: "Lin10961"
    },
    8: {
        dexcode: "SLN893827"
    },
    9: {
        dexcode: "Fen1016241"
    },
    10: {
        dexcode: "Aut331661"
    },
    11: {
        dexcode: "Pro39613"
    },
    12: {
        dexcode: "Com920158"
    },
    13: {
        dexcode: "Sma21322"
    },
    14: {
        dexcode: "Aut331661"
    },
    15: {
        dexcode: "Pro39613"
    },
    16: {
        dexcode: "Com920158"
    },
    17: {
        dexcode: "Sma21322"
    }
}

From the array $dexcode_comp I wish to return all the values that don't appear in $matches. Like the opposite of an array_intersect function.

What can I do to achieve this? I hope someone can push me in the right direction.

Rutwick Gangurde
  • 4,124
  • 8
  • 42
  • 77
Kevin Test
  • 123
  • 7
  • 3
    Those are objects. – Script47 Dec 05 '18 at 12:47
  • @Script47 give me a second, its because of this chrome extension im using. Turns arrays into objects. let me fix it lol – Kevin Test Dec 05 '18 at 12:50
  • Possible duplicate of [in\_array() and multidimensional array](https://stackoverflow.com/questions/4128323/in-array-and-multidimensional-array) – Cid Dec 05 '18 at 13:00
  • 1
    **SO is not a free coding service**. The idea is supposed to be... **YOU** code something to match your requirements and **THEN if** you get problems you ask a question about a specific coding problem here. – RiggsFolly Dec 05 '18 at 13:03
  • @RiggsFolly did i ask for code? I literally asked push me in the rigth direction. – Kevin Test Dec 05 '18 at 13:07
  • Its also not a tutorial site – RiggsFolly Dec 05 '18 at 13:08
  • So asking for a hint in the right direction is asking for a tutorial now? @RiggsFolly – Kevin Test Dec 05 '18 at 13:08
  • Welcome, to improve your experience on SO please read [how to ask](https://stackoverflow.com/help/how-to-ask) an [On Topic question](https://stackoverflow.com/help/on-topic), and the [Question Check list](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist) and [the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and how to create a [Minimal, Complete and Verifiable Example](http://stackoverflow.com/help/mcve) and [TAKE THE TOUR](http://stackoverflow.com/tour) – RiggsFolly Dec 05 '18 at 13:09

2 Answers2

1

Those are objects, not arrays, so you can use json_encode and json_decode with the second parameter as true to make then into associative arrays and then use array_diff_assoc:

$arr1 = json_decode(json_encode($obj1), true);
$arr2 = json_decode(json_encode($obj2), true);
$diff = array_diff_assoc($arr1, $arr2);

Note: There is also a array_diff if you are interested.

Reading Material

json_encode

json_decode

array_diff_assoc

Script47
  • 12,869
  • 4
  • 38
  • 59
1

You can use array-diff:

$result = array_diff($matches, $dexcode_comp);

This will compare the first array with the second and return an array with the values that are not in common. As you said the opposite of array_intersect

dWinder
  • 11,359
  • 3
  • 19
  • 33
pr1nc3
  • 6,575
  • 3
  • 15
  • 29