Question

I have 2 arrays, Array A and B respectively . Array A contains ~300,000 string records, e.g.

[0] => 'apple',
[1] => 'pineapple',
[2] => 'orange',
...
[299,999] => 'banana'

while Array B contains 100,000 string values, e.g.

[0] => 'bamboo',
[1] => 'banana',
[2] => 'boy',
[3] => 'ball',
[4] => 'balloon',
[5] => 'bazooka',

The question is, how to find out the common values between 2 arrays ?

array_intersect() seems a promising function, but I worry about the performance. Is it better to convert the 2 arrays into text file, and do file-based compare? or am I worrying too much?

Codes to use array_intersect():

$result_array = array_intersect($arrayA, $arrayB);
Was it helpful?

Solution

Result based on my own test, array_intersect() is the choice. It can produce the result in less than 1 second, as its efficiency is O(n·log n).

Reference: https://stackoverflow.com/a/6329494/188331

OTHER TIPS

array_intersect function will be used for retrieving common values across arrays

But as array size is huge you need to specify configuration in script for execution with concern to performance

    set_time_limit(0);
    ini_set('memory_limit','128M');

The above code snippet will respectively set the execution time limit to infinity and increasing memory limit will allocate more memory required to hold large sized array

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top