V
V
Vitaly Mironov2018-05-26 19:47:11
PHP
Vitaly Mironov, 2018-05-26 19:47:11

How to output matches from 2 arrays?

Good day.
How to quickly and correctly display elements of the first array repeated in another array?
For example:

$array = [
    "1" => "foo",
    "2" => "bar",
];

$array2 = [
    "1" => "def",
    "2" => "foo",
];

$array2 = [
    "1" => "dec",
    "2" => "foo",
];

Should output foo to another array.
I use this method - I compare in a loop all the elements of the array with the next array, and send all matches to another array. But this is a long time, taking into account the fact that there can be many arrays and the number of their elements can reach 10 million.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
ipokos, 2018-05-27
@nor1m

array_unique(array_intersect($array, $array2))
But as you were told above, it's better to divide such a volume...
and so, when testing for 100k elements in arrays (only numbers), the execution takes about 1MB of memory

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question