Array Difference Between Large Arrays (PHP)

Php

Array Difference Between Large Arrays (PHP)

Hi, today i’ve encounter a two large array to be compare. Array 1 is around 50k data and Array 2 is around 500k data. So what i wanna eliminate is , If Array2’s data exist in Array1 , i want it out from Array 2. So here’s what i will normally do (SLOW VERSION 🙁 boooo ….)

$Array1 = .........;
$Array2 = .........;

$Array2 = array_diff($Array2,$Array1);
$Array2 = array_values($Array2);

Now , i would like to perform a quick diff method (FASTER VERSION)

$Array1 = .........;
$Array2 = .........;

#Perform Fliiping
$Array1 = array_flip($Array1);
$Array2 = array_flip($Array2);

#Compare Using Keys
$Array2 = array_diff_key($Array2,$Array1);

#Finally , rearrange your array into normal array pattern
$Array1 = $array_keys($Array1);
$Array = $array_keys($Array2);

There you go , This is the result of If Array2’s values exist in Array1 It will be Filtering/removed/Unset out from Array2. Enjoy 🙂

Back To Top