如何循环遍历具有多个值的数组
我知道这个问题很简单,但我对数组缺乏经验,并且遇到困难。无论如何,我有这个数组:
$variables=array("$value1" => "int",$value2 => "var",$value3 => "int");
我想循环遍历它,并且对于每个 $value 我想添加
$stmt->bindValue(1,$value, PDO::PARAM_INT);
if $value是 'int' 或
$stmt->bindValue(1,$value);
如果 $value 是 'var'
并且在循环变量时 '1' 增加。有谁知道该怎么做?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许是这样的:
Maybe something like this:
foreach
循环可让您循环遍历数组。foreach ($array as $k=>$v)
语法可让您定义一个变量来保存当前键 ($k
) 和一个用于保存当前值 ($v
)。我还使用了一个在每个周期中递增的简单变量 ($i
)。然后使用简单的
if
-elseif
组合来执行不同的操作。这可以进一步优化,我想展示逻辑。
A
foreach
loop 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
-elseif
combo is used to do something different.This can be optimized further, I wanted to show the logic.
简单的循环应该可以做到
Simple loop should do it