Q:

How to use (or combine) multiple arrays for use in a foreach

I'm trying to display some text on a vbulletin forum if the thread starter is a member of usergroup 5, 6 or 7.  In the past, when I've used arrays, they normally only have one array out.  My current code has multiple arrays which I not sure how to use. The data stored in the fields usergroupid & membergroupids is one number based on the group id.
CentOS Linux 7.6.1810, phpmyadmin 4.8.4, Server version 5.5.60-MariaDB , PHP 7.1.29.
I have tried array_merge but because the values are from the database, I do not know how to give each array a key & value.  I believe each key and/or value must be unique to merge the arrays.
My code
$current_thread = $thread['threadid'];  

    $query = $vbulletin->db->query_first("  
        SELECT user.usergroupid, user.membergroupids  
        FROM " . TABLE_PREFIX . "thread AS thread  
        LEFT JOIN " . TABLE_PREFIX . "user AS user ON (user.userid = thread.postuserid)  
        WHERE thread.threadid = " . $current_thread . "  
        "); 

    $primary_group = $query['usergroupid'];  
    $secondary_groups = $query['membergroupids'];  

    if(!empty($secondary_groups)) {  
        $groups = $primary_group . "," . $secondary_groups;  
    } else {  
        $groups = $primary_group;  
    }      

    $data = explode(PHP_EOL, $groups); 

    foreach ($data AS $data_groups)  
    {
    $usergroup = array_map('trim', explode(',', $data_groups));  
    print("<pre>".print_r($usergroup,true)."</pre>");
    }

    vB_Template::preRegister('threadbit',array('group' => $groups_all));

The output
Array
(
    [0] => 6
)
Array
(
    [0] => 2
    [1] => 5
)
Array
(
    [0] => 6
)
Array
(
    [0] => 6
)
Array
(
    [0] => 2
    [1] => 5
)
Array
(
    [0] => 6
)
Array
(
    [0] => 6
)
Array
(
    [0] => 6
)
Array
(
    [0] => 2
    [1] => 5
)

The output above is based on 9 threads in the forum being viewed.  Is there a way I can check if the thread starter is in usergroup 5, 6 or 7 which I can then use later on?

A:

You can use array_intersect to check array values exists in 5,6,7
$result = [];
foreach ($data as $data_groups) {
    $usergroup = array_map('trim', explode(',', $data_groups));
    $temp      = array_intersect($usergroup, [5, 6, 7]); // checking if $usergroup 
                                                         //valus matches
    if (count($temp) > 0) {
        $result[] = $data_groups; 
        // if don't want complete data groups then
        //$result[] = $temp; // only matched groups will add
    }
}
// you can use $result for your requirement 

array_intersect — Computes the intersection of arrays

