I know this question is simple, but I have little experience with arrays and am having difficulty. Anyway I have this array:
$variables=array("$value1" => "int",$value2 => "var",$value3 => "int");
I want to cycle through it and for each $value I would like to add
$stmt->bindValue(1,$value, PDO::PARAM_INT); if $value is ‘int’ or
$stmt->bindValue(1,$value); if $value is ‘var’
and have the ‘1’ increase as it cycles through the variables. Does anyone know how to do this?
A
foreachloop lets you loop through your array. Theforeach ($array as $k=>$v)syntax lets you define a variable to hold the current key ($k) and one for the current value ($v). I also used a simple variable that is incremented in every cycle ($i).Then a simple
if–elseifcombo is used to do something different.This can be optimized further, I wanted to show the logic.