AND 在 PHP foreach 循环中?
foreach 循环中可以有 AND 吗?
例如,
foreach ($bookmarks_latest as $bookmark AND $tags_latest as $tags)
Is it possible to have an AND in a foreach loop?
For Example,
foreach ($bookmarks_latest as $bookmark AND $tags_latest as $tags)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您始终可以使用循环计数器来访问第二个数组中与在 foreach 循环中访问时相同的索引(我希望这是有意义的)。
例如:-
这应该实现您想要做的事情,否则使用 dark_charlie 建议的方法。
在 PHP 5 >= 5.3 中,您可以使用 MultipleIterator。
You can always use a loop counter to access the same index in the second array as you are accessing in the foreach loop (i hope that makes sense).
For example:-
That should achieve what you are trying to do, otherwise use the approach sugested by dark_charlie.
In PHP 5 >= 5.3 you can use MultipleIterator.
简短的回答:不。您始终可以将书签和标签放入一个数组中并对其进行迭代。
或者你也可以这样做:
编辑:
所请求的单阵列解决方案示例:
Short answer: no. You can always put the bookmarks and tags into one array and iterate over it.
Or you could also do this:
EDIT:
The requested example for the one-array solution:
你不能那样做。
但是
,如果您有一个
hash
类型数组,但如果您在数组中有嵌套值,则上面的示例效果非常好,我建议您:或选项 2
you can't do that.
but you can
the above example works really well if you have a
hash
type array but if you have nested values in the array I recommend you:or option 2
不,但是有很多方法可以做到这一点,例如:
No, but there are many ways to do this, e.g:
不,不,不是。
您必须手动编写一个循环,使用索引或内部指针同时遍历两个数组。
No. No, it is not.
You'll have to manually write out a loop that uses indexes or internal pointers to traverse both arrays at the same time.
是的,为了完整起见:
这将是获得你想要的东西的原生方式。但显然,只有两个输入数组的长度完全相同时它才有效。
(然而,使用单独的迭代键是更常见的方法。)
Yes, for completeness:
That would be the native way to get what you want. But it only works if both input arrays have the exact same length, obviously.
(Using a separate iteration key is the more common approach however.)