Query containing array
Option 1 - Use a temporary table

CREATE TEMPORARY TABLE foo (`id` INT, `variable` VARCHAR(255), PRIMARY KEY (`id`));


Insert the data into the primary table and do a normal JOIN.

Note that temporary tables are created in memory and are dropped when the connection is closed (when the php script is done).

Option 2 - Query and add to result

Assuming that $vars are key/value pairs of id/variable

$ids = join(',', array_keys($vars));
$result = $db-&gt;query("SELECT * FROM `fields` WHERE `id` IN ($ids)");

$data = [];
while ($row = $result-&gt;fetch_assoc()) {
    $variable = $vars[$row['id']];
    $data[] = $row + ['variable' =&gt; $variable];
}