Using array_combine()
, you can create a new array, specify one array as the keys, the other as values in the new array. Then it's just a matter of looping the resulting array, and execute queries. Since you now have one array, use a foreach
loop and use the keys (in this case, all the values from $a
) and values (in this case, all the values from $b
) as the values you're setting.
This assumes that the amount of entries in both array is always the same. If they are not of the same size, array_combine()
will return false - you can use that as a check before performing the queries.
$a = [1, 2, 3, 4];
$b = [10, 20, 30, 40];
$result = array_combine($a, $b);
foreach ($result as $k=>$v) {
mysql_query("UPDATE table SET corr='$k' WHERE id = '$v'");
}
That being said, this query is vulnerable to SQL injection, and you should upgrade to a newer API which supports parameterized queries with placeholders (mysqli_*
or PDO). The mysql_*
API was deprecated in PHP 5.6 and removed entirely in PHP7.