AND 在 PHP foreach 循环中?

发布于 2024-11-14 23:47:12 字数 133 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

骄兵必败 2024-11-21 23:47:12

您始终可以使用循环计数器来访问第二个数组中与在 foreach 循环中访问时相同的索引(我希望这是有意义的)。

例如:-

$i = 0;
foreach($bookmarks_latest as $bookmark){
   $result['bookmark'] = $bookmark;
   $result['tag'] = $tags_latest[$i];
   $i++;
}

这应该实现您想要做的事情,否则使用 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:-

$i = 0;
foreach($bookmarks_latest as $bookmark){
   $result['bookmark'] = $bookmark;
   $result['tag'] = $tags_latest[$i];
   $i++;
}

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.

骷髅 2024-11-21 23:47:12

简短的回答:不。您始终可以将书签和标签放入一个数组中并对其进行迭代。

或者你也可以这样做:

reset($bookmarks_latest);
reset($tags_latest);
while ((list(, $bookmark) = each($bookmarks_latest)) && (list(,$tag) = each($tags_latest)) {
    // Your code here that uses $bookmark and $tag
}

编辑:
所请求的单阵列解决方案示例:

class BookmarkWithTag  {
  public var $bookmark;
  public var $tag;
}

// Use the class, fill instances to the array $tagsAndBookmarks


foreach ($tagsAndBookmarks as $bookmarkWithTag)  {
  $tag = $bookmarkWithTag->tag;
  $bookmark = $bookmarkWithTag->bookmark;
}

Short answer: no. You can always put the bookmarks and tags into one array and iterate over it.

Or you could also do this:

reset($bookmarks_latest);
reset($tags_latest);
while ((list(, $bookmark) = each($bookmarks_latest)) && (list(,$tag) = each($tags_latest)) {
    // Your code here that uses $bookmark and $tag
}

EDIT:
The requested example for the one-array solution:

class BookmarkWithTag  {
  public var $bookmark;
  public var $tag;
}

// Use the class, fill instances to the array $tagsAndBookmarks


foreach ($tagsAndBookmarks as $bookmarkWithTag)  {
  $tag = $bookmarkWithTag->tag;
  $bookmark = $bookmarkWithTag->bookmark;
}
提笔落墨 2024-11-21 23:47:12

你不能那样做。

但是

<?php
foreach($keyval as $key => $val) {
  // something with $key and $val
}

,如果您有一个 hash 类型数组,但如果您在数组中有嵌套值,则上面的示例效果非常好,我建议您:

或选项 2

<?php
foreach ($keyval as $kv) {
 list($val1, $val2, $valn) = $kv;
}

you can't do that.

but you can

<?php
foreach($keyval as $key => $val) {
  // something with $key and $val
}

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

<?php
foreach ($keyval as $kv) {
 list($val1, $val2, $valn) = $kv;
}
嘿咻 2024-11-21 23:47:12

不,但是有很多方法可以做到这一点,例如:

reset($tags_latest);
foreach ($bookmarks_latest as $bookmark){
    $tags = current($tags_latest); next($tags_latest);
    // here you can use $bookmark and $tags
}

No, but there are many ways to do this, e.g:

reset($tags_latest);
foreach ($bookmarks_latest as $bookmark){
    $tags = current($tags_latest); next($tags_latest);
    // here you can use $bookmark and $tags
}
左耳近心 2024-11-21 23:47:12

不,不,不是。

您必须手动编写一个循环,使用索引或内部指针同时遍历两个数组。

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.

羁〃客ぐ 2024-11-21 23:47:12

是的,为了完整起见:

foreach (array_combine($bookmarks_latest, $tags_latest) as $bookm=>$tag)

这将是获得你想要的东西的原生方式。但显然,只有两个输入数组的长度完全相同时它才有效。

(然而,使用单独的迭代键是更常见的方法。)

Yes, for completeness:

foreach (array_combine($bookmarks_latest, $tags_latest) as $bookm=>$tag)

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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文