Hello to all colleagues, hear the cry of the soul please. How to get the difference of two arrays trace type:

First array

Array ( [0] => Array ( [ID] => 21323154 [NAME] => Имя_2 [PREVIEW_TEXT] => Текст_2 ) ) 

Second array

 Array ( [0] => Array ( [ID] => 543547564 [NAME] => Имя_1 [PREVIEW_TEXT] => Текст_1 ) [1] => Array ( [ID] => 222213322 [NAME] => Имя_2 [PREVIEW_TEXT] => Текст_2 ) [2] => Array ( [ID] => 333876833 [NAME] => Имя_3 [PREVIEW_TEXT] => Текст_3 ) ) 

How to cut the second element from the second array, the ID is different, can only be compared by NAME or PREVIEW_TEXT .

I used the array_diff_assoc function, I do not know how to add another difference search condition by key, well, logically, like this:

 $ARR_result_Merge = array_diff_assoc($array_FIRST['NAME'], $array_SECOND['NAME']); // глупо конечно, но как сделать правильно? 
  • @eicto maybe you have a thought, I have one element returned, the one that we cut out, but it’s necessary that that array with values ​​except the one that was cut out. - Artyomich

3 answers 3

Here you need to use the array_udiff function

 function key_compare_func($a, $b) { if ($a['NAME'] === $b['NAME'] && $a['PREVIEW_TEXT'] === $b['PREVIEW_TEXT']) { return 0; } return ($a['NAME'] > $b['NAME'] && $a['PREVIEW_TEXT'] > $b['PREVIEW_TEXT'])? 1:-1; } $result = array_udiff($array2, $array1, "key_compare_func"); print_r($result); 
  • @evz gives an error for some reason Fatal error: Cannot redeclare key_compare_func () - Artyomich
  • one
    @Artyomich, how many times does a file with a function connect? - etki
  • @Fike ahhh exactly, maybe five times ... can describe a separate method? and then how to call it? - Artyomich
  • one
    @Artyomich, write with an anonymous function or render to an earlier file. - etki
  • @Fike as in the example of @Alexander Deider - Artyomich

Try array_udiff .
Sort of:

 array_udiff($a1, $a2, function($a, $b){ return (int) ($a['NAME']===$b['NAME'] && $a['PREVIEW_TEXT']===$b['PREVIEW_TEXT']); }); 
  • @Alexander Deider I need a little bit all the way around, I need the second array to be displayed except for the match from the first one, your example fits, but you need to return, tell me how you can do it? - Artyomich

Colleagues, thank you all for participation, all for good, all great, I don’t want to leave the question without a final answer. In the end, I needed the following, the output should be an array of the form:

 Array ( [0] => Array ( [ID] => 543547564 [NAME] => Name_1 [PREVIEW_TEXT] => Text_1 ) [1] => Array ( [ID] => 333876833 [NAME] => Name_3 [PREVIEW_TEXT] => Text_3 ) ) 

The solution comes down to the example:

 $output = array_udiff($arrayFirst, $arraySecond, function($a, $b){ return ($a['NAME'] == $b['NAME']) ? 0 : -1; });