Answer the question
In order to leave comments, you need to log in
How to work with large arrays in php?
Good afternoon.
I receive arrays with numbers from a third-party resource.
The goal is to compare two such arrays using, for example, array_intersect.
The problem is that there can be millions of these numbers in the array. And these processes can be several in a unit of time.
Please tell me how to organize the work in such a way that it does not eat all the memory.
Answer the question
In order to leave comments, you need to log in
I think the most reasonable solution would be to write these arrays to the database and compare with a simple query. This is how, in the general case, the problem of "comparing two large data arrays in PHP" is solved. A specific implementation can be offered only if there is a specific question, without "maybe" and "for example".
Yes, the memory in the database is also not free, but here we must already decide - either we want to toss gigas of warez, or sit on a penny VPS with 500 meters of memory. It won't work at the same time.
Of course, you can dodge and compile a special extension , which is just intended to work with illimons of elements, but again, it all depends on the capabilities and needs.
To not in memory, you can disk. Save an array of numbers to a file. For compactness, as a binary file, 4 bytes per pack() number (if this is a 32-bit VKontakte id, you compare. Facebook id is long). Sorted. The same VK is able to give the list sorted. Save the second one as well.
Open both files, read fread($fh, 4) from each by number: number A, number B.
Is A greater than B ? Reading new B.
B more than A ? Reading the new A.
Ravny? Hooray, we save this number in the "crossing". We read new A and new B.
One of the files has ended - there are no more intersections.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question