Answer the question
In order to leave comments, you need to log in
How to process 2 arrays less resource-intensive?
Good afternoon.
There are 2 arrays
$array_first and $array_second
I need to pull out all the values that are in both arrays + their keys.
Now it is solved like this
foreach($array_first as $key => $value)
{
if (in_array($value, $array_second, true))
{
$key_second = array_search($value, $array_second);
// тут засовываем все в отдельный массив
}
Answer the question
In order to leave comments, you need to log in
array_intersect_assoc
// EDIT
<?php
$foo = [
'alice' => 'bob',
'bar' => 'baz',
];
$bar = [
'john' => 'doe',
'foo' => 'baz',
];
var_dump(
array_intersect($foo, $bar),
array_intersect($bar, $foo)
);
// output:
array(1) {
'bar' =>
string(3) "baz"
}
array(1) {
'foo' =>
string(3) "baz"
}
Sort each array by value; move through both arrays, catching matching values and their keys.
And sorting can be done with minimal memory cost, in chunks. And the second phase, comparison - it is enough to store the current values of each of the arrays in memory.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question