-2

I have 2 array of std objects where i need to filter out array -> stdClass Object -> matching [code] and get final array with unique array -> stdClass Object -> [code] here are examples array #1

Array
(
    [0] => stdClass Object
        (
            [code] => 100
            [c_price] => 438
        )

    [1] => stdClass Object
        (
            [code] => 1100
            [c_price] => 105
        )

)

here are examples array #2

Array
(
    [0] => stdClass Object
        (
            [code] => 100
            [c_price] => 1250
        )

    [1] => stdClass Object
        (
            [code] => 1100
            [c_price] => 300
        )

    [2] => stdClass Object
        (
            [code] => 4807
            [c_price] => 1000
        )

)

Expected results i want to get

Array
(

    [0] => stdClass Object
        (
            [code] => 4807
            [c_price] => 1000
        )

)

i have tried many answers but not found any closer to my problem, i have tried array_unique but it's not working because of std object class, thanks in advance

uzthegeek
  • 43
  • 6
  • Explain what you are trying to do. What is the relationship between array #1 and array #2? Why is that the expected result? Any code? Are you simply looking for [array_diff](https://www.php.net/manual/en/function.array-diff.php)? – ficuscr Feb 24 '22 at 04:40
  • 1
    Does this answer your question? [difference between two arrays](https://stackoverflow.com/questions/10077840/difference-between-two-arrays) - oh, realize the stdObject aspect might throw you off. Still stuck? Look at https://stackoverflow.com/questions/19495068/convert-stdclass-object-to-array-in-php for a quick fix. Otherwise iterate on collection. – ficuscr Feb 24 '22 at 04:43
  • What about array_diff_assoc()? – Kiran Rai Chamling Feb 24 '22 at 04:48
  • https://stackoverflow.com/a/19495142/18078362 remove stdclass and then try array_unique – pk_ Feb 24 '22 at 04:49
  • @KiranRaiChamling please show me example of array_diff_assoc() i need to use it in codeignitor i need to filter duplicates and return only unique just as example – uzthegeek Feb 24 '22 at 04:50
  • @pk_ i cannot remove std object i need output 100% same as Expected – uzthegeek Feb 24 '22 at 04:52
  • what is the use of stdclass??? – pk_ Feb 24 '22 at 04:53
  • @pk_ this is requirement there's lot of code i show you short example if you can please answer the question properly within criteria – uzthegeek Feb 24 '22 at 04:55
  • you first ask the question properly – pk_ Feb 24 '22 at 04:56
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/242347/discussion-between-uzthegeek-and-pk). – uzthegeek Feb 24 '22 at 05:00
  • This question is comparing two-dimensional data. This means that [difference between two arrays](https://stackoverflow.com/q/10077840/2943403) is incorrect advice. Please specifically see my advice at https://stackoverflow.com/a/71049380/2943403 You will need to isolate `$a->code` and `$b->code`. – mickmackusa Feb 24 '22 at 06:34

1 Answers1

-2

solved with

$arrdiff = array_diff_assoc($array2, $array1);

cheers @ Kiran Rai Chamling

uzthegeek
  • 43
  • 6
  • This only works by happenstance. This is not a reliable solution. – mickmackusa Feb 24 '22 at 06:31
  • You can read about the folly of this answer here: [While comparing associative rows between two 2d arrays, array_diff_assoc() gives the wrong difference](https://stackoverflow.com/a/9885366/2943403) – mickmackusa Feb 24 '22 at 06:44