我可以使用 foreach 迭代两个大小相等的循环吗?

发布于 2024-11-01 05:20:33 字数 424 浏览 1 评论 0原文

我是 PHP 新手。我有两个大小相等的数组 $array1$array2 。我一直在使用 foreach 循环来迭代数组,如下所示:

foreach($array1 as $element1) {
      //Do stuff with $element1
}

foreach($array2 as $element2) {
      //Do stuff with $element2
}

现在我想同时迭代两个数组,以便我可以访问两个 $element1< /code> 和 $element2 在循环体中。

我该怎么做?

I'm new to PHP. I have two arrays $array1 and $array2 of equal size. I've been using foreach loops to iterate through arrays like so:

foreach($array1 as $element1) {
      //Do stuff with $element1
}

and

foreach($array2 as $element2) {
      //Do stuff with $element2
}

but now I'd like to iterate through both arrays at the same time so that I have access to both $element1 and $element2 in the loop body.

How do I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

自由范儿 2024-11-08 05:20:33
while (($element1 = next($array1)) !== false) {
  $element2 = next($array2);
  // Do something
}

但如果 false$array1 中允许的值,它将失败。如果(在这种情况下)$array2中不允许false,则可以交换两个

“foreach”解决方案(如果两者共享相同的密钥)

foreach ($array1 as $i => $element1) {
  $element2 = $array2[$i];
  // Do something
}

第三个(我认为相当不错)的解决方案,只允许 $array1 中的原始类型

foreach (array_combine(array_values($array1), array_values($array2)) as $element1 => $element2) {
  // Do something
}
while (($element1 = next($array1)) !== false) {
  $element2 = next($array2);
  // Do something
}

But it will fail, if false is an allowed value in $array1. If (in this case) false is not allowed in $array2, you can just swap both

A "foreach"-solution (if both shares the same key)

foreach ($array1 as $i => $element1) {
  $element2 = $array2[$i];
  // Do something
}

A third (I think quite nice) solution, that just allows primitive types in $array1

foreach (array_combine(array_values($array1), array_values($array2)) as $element1 => $element2) {
  // Do something
}
风筝有风,海豚有海 2024-11-08 05:20:33

Each 返回一个包含键和值的数组,并将指针前进到下一个元素。一旦超过最后一个元素,它就会返回 false。

// Iterate until one of the arrays is complete
while (($a = each($array_a)) !== false && ($b = each($array_b)) !== false) {
    echo "The key:value from array_a is {$a['key']}:{$a['value']}.\n";
    echo "The key:value from array_b is {$b['key']}:{$b['value']}.\n";
}

要完全迭代两个数组,请使用 ||而不是 &&。

Each returns an array containing the key and value, and advances the pointer to the next element. It returns false once it has advanced past the last element.

// Iterate until one of the arrays is complete
while (($a = each($array_a)) !== false && ($b = each($array_b)) !== false) {
    echo "The key:value from array_a is {$a['key']}:{$a['value']}.\n";
    echo "The key:value from array_b is {$b['key']}:{$b['value']}.\n";
}

To iterate completely over both arrays, use || instead of &&.

鼻尖触碰 2024-11-08 05:20:33

使用 for 循环代替...

for($i = 0;$i<count($array1);$i++) { 
    /* access $array1[$i] and $array2[$i] here */ 
}

如果数组的索引是数字并且两个数组都相同,则这将起作用

use a for loop instead...

for($i = 0;$i<count($array1);$i++) { 
    /* access $array1[$i] and $array2[$i] here */ 
}

This will work if the indexes of the arrays are numeric and the same for both arrays

浊酒尽余欢 2024-11-08 05:20:33

你显然

$i = 0;
foreach($array1 as $element) {

    // Do stuff with the element
    $stuff_from_this_array = $element;
    $stuff_from_other_array = $array2[$i];
    $i++;

}

想将 $stuff_this_array 和 $stuff_from_other_array 放入更持久的东西中,但这也许给你一个想法。

What about

$i = 0;
foreach($array1 as $element) {

    // Do stuff with the element
    $stuff_from_this_array = $element;
    $stuff_from_other_array = $array2[$i];
    $i++;

}

You'd obviously want to put $stuff_this_array and $stuff_from_other_array into something more persistent, but maybe this gives you an idea.

栖竹 2024-11-08 05:20:33

这是一个可能的解决方案。如果直接从 next() 开始,则永远不会获得数组的第一个元素。

reset($array1); reset($array2);
for ($element1 = current($array1), $element2 = current($array2); 
        $element1 !== false && $element2 !== false; 
        $element1 = next($array1), $element2 = next($array2)) {
    // Do something

}

That is a possible solution. If you start with next() directly, you never get the first element of the array.

reset($array1); reset($array2);
for ($element1 = current($array1), $element2 = current($array2); 
        $element1 !== false && $element2 !== false; 
        $element1 = next($array1), $element2 = next($array2)) {
    // Do something

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