I am using a foreach loop to populate an array with parameter types and parameter values before it inserts into the database. Everything works fine and posts to the db, but I can't figure out why it sends both values as the same when they are added (as a reference) to the array. I have tired calling unset(); but it doesn't do the trick.
function insertUsers($db, $sql, $vals){
$stmt= $db->prepare($sql);
$types='';
$types= $types ?: str_repeat("s", count($vals));
$inputArray [] = &$types;
foreach($vals as $key=>$value){
$inputArray[]= &$value;
unset($key);
}
print_r($inputArray);
call_user_func_array(array($stmt, 'bind_param'), $inputArray);
$stmt->execute();
$stmt->close();
}
The result ends being:
Array
(
[0] => ss
[1] => tyuty (note this value changes to the last value)
[2] => tyuty
)
&on every step. I.e.insertUsers($db, $sql, &$vals),foreach($vals as $key=>&$value).