1

I have two multidimensional arrays:

$arr_one = [
    [
        'param_one' => 'qwe',
        'param_two' => 'yellow'
    ],[
        'param_one' => 'xyz',
        'param_two' => 'blue'
    ],[
        'param_one' => 'abc',
        'param_two' => 'pink'
    ]
    ...
];

$array_two = [
    [
        'param_one' => 'xyz',
        'param_two' => 'blue'
    ],[
        'param_one' => 'abc',
        'param_two' => 'pink'
    ]
    ...
];

Each one of this arrays has around 20.000 elements in it. I want to see which elements (sub-array) -including all its elements - is in the first array (array_one) but is not present in the second array (array_two).

For this i've made this function:

function search_arr($needle, $haystack){
    $needle = array_values($needle);

    foreach ($haystack as $k => $v){

        if ( array_search($needle[0], $v) ){

            if ( array_search($needle[1], $v) ){
                return true;
            }
        }
    }
    return false;
}

And i am using it this way:

foreach($array_one as $arr){
    if(!search_arr($arr, $array_two)){
        $unique[] = $arr;
    }
}

It works well for small arrays but in my case because each array has around 20.000 sub-arrays it takes forever to go trough all of them :(

Is there a solution that will allow me to check which sub-array (with all its elements) exists in array_one but doesn't exist in array_two?

Melody
  • 127
  • 10
  • 1
    If they're in the same order and keys are also the same https://stackoverflow.com/questions/22354767/use-array-diff-assoc-or-get-difference-of-multidimensional-arrays/22355153#22355153 – AbraCadaver May 14 '20 at 20:34
  • @bestprogrammerintheworld hey, thank you but this answer is not ok for me because it uses only one column. That is quiet simple but i need something that compares the whole sub-arrays. – Melody May 14 '20 at 20:40
  • 2
    @AbraCadaver thank you! That accepted answer is amazing and it reduced the time from ~600 seconds to ~10 :O – Melody May 14 '20 at 20:58

0 Answers0