如何在 while 循环中使用多个 list()/each() 调用?
我正在使用 3 个不同的数组(尽管我暂时只使用两个进行测试),并且我正在尝试处理 $_POST
上的数组。我当前正在使用:
while(list($key_member,$member)=each($_POST['member_ids'])
&& list($key_amount,$amount)=each($_POST['payment_amounts']))
{
echo "MEMBER: $member<br>";
echo "AMOUNT: $amount<br><br>";
}
如果我在任一数组上使用一个 list()
,它将打印该特定项目的信息。但是,如果我尝试同时使用多个 list()
命令,则只有最后一个 list()
项目会被正确填充。 list()
是否在后台做了一些诡计,导致它无法在 while 循环中工作?
显然,“简单”的解决方案是使用索引并简单地强制解决问题,但我更喜欢枚举 - 老实说,我只是好奇
我做错了什么,和/或 列表()?
I'm working with 3 different arrays (although I'm only testing with two for the time being) and I'm trying to process the arrays on $_POST
. I'm currently using:
while(list($key_member,$member)=each($_POST['member_ids'])
&& list($key_amount,$amount)=each($_POST['payment_amounts']))
{
echo "MEMBER: $member<br>";
echo "AMOUNT: $amount<br><br>";
}
If I use one list()
on either array it will print the info for that particular item. However, if I attempt to use multiple list()
commands in the while, only the last list()
ed item gets filled properly. Is list()
doing some trickery in the background that's preventing it from working in a while loop?
Obviously the "easy" solution would be to use an index and simply force the issue, but I prefer enumerating -- and I'm honestly just curious as to
What am I doing wrong, and/or what is "broken" with list()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
漏洞?不知道。
这是一个解决方法。
bug? dunno.
here's a workaround.
您可以使用 array_keys() 提取每个数组的键,它会生成一个索引数组,然后为每个数组保留单独的循环计数器:
You could extract each array's keys using array_keys(), which produces an indexed array, then keep separate loop counters for each array:
&&
以短路方式求值,第一个返回 false 的语句会跳出它。在您的情况下,一旦第一个数组结束,它就会停止迭代。list
在这里应该可以正常工作,因为它是分配变量的语言构造。&&
is evaluated in a short-circuit manner, the first statement to return false jumps out of it. In your case it stops to iterate as soon as the first array is at its end.list
should work fine here, as it's a language construct which assigns variables.