在 PHP 中有效使用 foreach

发布于 2025-01-01 10:54:36 字数 970 浏览 0 评论 0原文

所以我认为我没有充分利用 foreach 循环。这是我对foreach的理解。

  • 它就像 foreach(arrayyouwanttoloopthrough as onevalueofthatarray)

  • 不需要计数器或递增,它会自动提取一个数组,每个循环逐个值,并且对于该循环它调用“as”之后的值。

  • 处理完数组后停止。

  • 只要处理数组,就应该基本上替换“for”。

因此,我尝试用 foreach 做的很多事情是修改循环数组中的数组值。但最终我不断发现我必须使用 for 循环来处理此类事情。

假设我有一个数组(thing1、thing2、thing3、thing4)并且我想更改它......让我们对所有“BLAH”说,末尾有一个数字,我会这样做

$counter = 0;
foreach($array as $changeval){
$counter++;
$changeval = "BLAH".$counter;
}

我会认为会改变它,因为 $changeval 应该是数组的任何值,对吧?但事实并非如此。我能找到在 foreach 中做到这一点的唯一方法是设置一个计数器(如上所示),并使用带有计数器索引的数组。但要做到这一点,我必须将计数器设置在循环之外,而且它甚至并不总是可靠的。为此,我认为使用 for 循环而不是 foreach 会更好。

那么为什么要使用 foreach 呢?我想我在这里遗漏了一些东西,因为 foreach 必须能够更改值...

谢谢

哦嘿还有一件事。循环中设置的变量(如 i 或 key)是否可以在循环外部访问?如果我有 2 个 foreach 循环,例如

foreach(thing as value) ,

我是否必须制作第二个

foreach(thing2 as value2) ]

,否则会出现一些问题?

So I don't think I'm making full use of the foreach loop. Here is how I understand foreach.

  • It goes like foreach(arrayyouwanttoloopthrough as onevalueofthatarray)

  • No counter or incrementing required, it automatically pulls an array, value by value each loop, and for that loop it calls the value whatever is after the "as".

  • Stops once it's done with the array.

  • Should basically replace "for", as long as dealing with an array.

So something I try to do a lot with foreach is modify the array values in the looping array. But in the end I keep finding I have to use a for loop for that type of thing.

So lets say that I have an array (thing1, thing2, thing3, thing4) and I wanted to change it....lets say to all "BLAH", with a number at the end, I'd do

$counter = 0;
foreach($array as $changeval){
$counter++;
$changeval = "BLAH".$counter;
}

I would think that would change it because $changeval should be whatever value it's at for the array, right? But it doesn't. The only way I could find to do that in a] foreach is to set a counter (like above), and use the array with the index of counter. But to do that I'd have to set the counter outside the loop, and it's not even always reliable. For that I'd think it would be better to use a for loop instead of foreach.

So why would you use foreach over for? I think I'm missing something here because foreach has GOT to be able to change values...

Thanks

OH HEY One more thing. Are variables set in loops (like i, or key) accessible outside the loop? If I have 2 foreach loops like

foreach(thing as value)

would I have to make the second one

foreach(thing2 as value2) ]

or else it would have some problems?

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

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

发布评论

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

评论(5

场罚期间 2025-01-08 10:54:37

您可以使用引用变量来代替:

foreach ($array as &$value)
{
    $value = "foo";
}

现在数组已充满 foo(请注意 $value 之前的 &)。

通常,循环变量只包含相应数组元素的副本,但 &(与符号)告诉 PHP 将其作为对实际数组元素的引用,而不仅仅是副本;因此你可以修改它。

但是,正如 @Tadeck 下面所说,在这种情况下,您应该小心,在循环完成后销毁引用,因为 $value 仍将指向数组中的最后一个元素(因此可以不小心修改了)。使用 unset 执行此操作:

unset($value);

另一个选项是使用 $key =>; $value 语法:

foreach ($array as $key => $value)
{
    $array[$key] = "foo";
}

回答你的第二个问题:是的,它们随后可以在循环外部访问,这就是为什么在使用引用循环变量时最好使用 unset (如我的第一个示例中所示) 。但是,无需在后续循环中使用新的变量名称,因为旧的变量名称将被覆盖(不会产生不良后果)。

You can use a reference variable instead:

foreach ($array as &$value)
{
    $value = "foo";
}

Now the array is full of foo (note the & before $value).

Normally, the loop variable simply contains a copy of the corresponding array element, but the & (ampersand) tells PHP to make it a reference to the actual array element, rather than just a copy; hence you can modify it.

However, as @Tadeck says below, you should be careful in this case to destroy the reference after the loop has finished, since $value will still point to the final element in the array (so it's possible to accidentally modify it). Do this with unset:

unset($value);

The other option would be to use the $key => $value syntax:

foreach ($array as $key => $value)
{
    $array[$key] = "foo";
}

To answer your second question: yes, they are subsequently accessible outside the loop, which is why it's good practice to use unset when using reference loop variables as in my first example. However, there's no need to use new variable names in subsequent loops, since the old ones will just be overwritten (with no unwanted consequences).

罗罗贝儿 2025-01-08 10:54:37

您想通过引用传递:

$arr = array(1, 2, 3, 4);

foreach ($arr as &$value)
{
    $value = $value * 2;
}

// $arr is now array(2, 4, 6, 8)

You want to pass by reference:

$arr = array(1, 2, 3, 4);

foreach ($arr as &$value)
{
    $value = $value * 2;
}

// $arr is now array(2, 4, 6, 8)
感受沵的脚步 2025-01-08 10:54:37

扩展的foreach语法是这样的:

foreach ($array as $key => $value) {
}

使用$key,你可以索引原始数组:

foreach ($array as $key => $value) {
    $array[$key] = "BLAH";
}

The extended foreach syntax is like this:

foreach ($array as $key => $value) {
}

Using the $key, you can index the original array:

foreach ($array as $key => $value) {
    $array[$key] = "BLAH";
}
故事和酒 2025-01-08 10:54:37

从 0 递增到 ...

foreach($array as $changeval){
  if (!isset($counter)) { $counter = 0; }
  $counter++;
  $changeval = "BLAH".$counter;
}

使用 ARRAY 的索引/键

foreach($array as $key => $changeval){
  $changeval = "BLAH".$key;
}

Incrementing from 0 to ...

foreach($array as $changeval){
  if (!isset($counter)) { $counter = 0; }
  $counter++;
  $changeval = "BLAH".$counter;
}

Using the index/key of the ARRAY

foreach($array as $key => $changeval){
  $changeval = "BLAH".$key;
}
橙味迷妹 2025-01-08 10:54:37

您可以在使用 foreach 循环时使用该键:

foreach ($array as $key => $value)
{
    $array[$key] = "foo";
}

但请注意,在这种情况下,使用 Will suggest 之类的参考会更快- 但无论如何,$key => $value - 语法有时非常有用。

You can use the key when looping with foreach:

foreach ($array as $key => $value)
{
    $array[$key] = "foo";
}

But note that using a reference like Will suggested will be faster for such a case - but anyway, the $key => $value-syntax is quite useful sometimes.

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