How to find the intersection of arrays?

Now using array_merge() we combine in 1 array:
$result = [];
for($i = 0; $i < count($sql_query); $i++) {
 $query_result = $wpdb->get_results($sql_query[$i]);
 $result = array_merge($result, $query_result);
}

Arrays $query_result have this form:
Array
(
 [0] => stdClass Object
(
 [ID] => 481
)
 [1] => stdClass Object
(
 [ID] => 484
)
 [2] => stdClass Object
(
 [ID] => 485
)
)

Second example:
Array
(
 [0] => stdClass Object
(
 [ID] => 484
)
)

How to write the crossing? To $result was ID that is in each of arrays $query_result?
July 12th 19 at 17:05
3 answers
July 12th 19 at 17:07
Solution
$ids1 = array_column($result1, 'id');
$ids2 = array_column($result2, 'id');
// ...
$ISDN = array_column($resultn, 'id');

$intersection = array_intersect($ids1, $ids2, ..., $ISDN);


Or if we simplify:

$ids = array_intersect(
 ...array_map(function(array $collection) {
 return array_column($collection, 'id');
 }, $results)
);


updated:

$result = null;
foreach($sql_queries as $sql_query) {
 $query_result = $wpdb->get_results($sql_query);
 $ids = array_column($query_result, 'id');
 if (null === $result) { 
 $result = $ids; 
 } else {
 $result = array_intersect(
 $result, $ids
);
}
 $result = array_merge($result, $query_result);
}
$query_result outputs the array as in the example, $ids = array_column($query_result, 'id'); then gives an empty array - cristal_Littel commented on July 12th 19 at 17:10
: open Doc function, understand what they do, and then provera what's wrong with you. Immediately I can say that in my example "id" and you have "ID". - broderick_Gleas commented on July 12th 19 at 17:13
July 12th 19 at 17:09
For comparison, I guess you need to use php.net/array_flip but realize did not work - cristal_Littel commented on July 12th 19 at 17:12
: array_flip — exchanges keys with their values in the array, what's the comparison? - broderick_Gleas commented on July 12th 19 at 17:15
: like you need to change in order to compare on the ID? as in the example =484 - cristal_Littel commented on July 12th 19 at 17:18
July 12th 19 at 17:11

Find more questions by tags PHP