php 将值添加到 foreach 循环内的数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28216877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:45:04  来源:igfitidea点击:

Add values to an array inside a foreach loop

phparraysforeach

提问by zessx

I'm trying to edit an array on the fly, inside a foreachloop. I basically analyse each key, and if this key match the one I want, I want to add another entry in the array immediately after this one.

我正在尝试在foreach循环内动态编辑数组。我基本上分析了每个键,如果这个键与我想要的键匹配,我想在这个键之后立即在数组中添加另一个条目。

If I take this code,

如果我拿这个代码,

$values = array(
    'foo' => 10,
    'bar' => 20,
    'baz' => 30
);

foreach($values as $key => $value){
    print $value . ' ';
    if($key == 'bar'){
        $values['qux'] = 21;
    }
}

I've got 2 problems,

我有2个问题,

  • first, the output is 10 20 30instead of the expected 10 20 30 21
  • second, even if I solve the first problem, my value will still be added at the endof my array
  • 首先,输出10 20 30不是预期的10 20 30 21
  • 第二,即使我解决了第一个问题,我的值仍然会添加我的数组末尾

How could I add the quxentry between barand bazones?

我怎么能quxbar和之间添加条目baz

Thanks for your ideas.

谢谢你的想法。

回答by Lars Ebert

Foreach will not loop through new values added to the array while inside the loop.

Foreach 不会在循环内遍历添加到数组中的新值。

If you want to add the new value between two existing values, you could use a second array:

如果要在两个现有值之间添加新值,可以使用第二个数组:

$values = array(
    'foo' => 10,
    'bar' => 20,
    'baz' => 30
);
$newValues = array();
foreach($values as $key => $value) 
{
    $newValues[$key] = $value;
    if($key == 'bar') 
    {
        $newValues['qux'] = 21;
    }
}
print implode(' ', $newValue);

Also, see one of my favorite questions on StackOverflow discussing the foreach loop: How does PHP 'foreach' actually work?

另外,请参阅我在 StackOverflow 上讨论 foreach 循环时最喜欢的问题之一:PHP 'foreach' 实际上是如何工作的?

回答by Radiumrasheed

You can use the ampersand sign before the value.

您可以在值前使用与号。

//populate all the promos into their promoG groups
foreach($unclaimedPromoGroups as &$unclaimedPromoGroup) {
    $_promo = new Promo();
    $_promo->promoGroupID = $unclaimedPromoGroup['promo_groupID'];
    $promo = $_promo->getGroupPromos();
    $unclaimedPromoGroup["promos"] = $promo;
}

回答by Parag Tyagi -morpheus-

For this you need to create a new array,

为此,您需要创建一个新数组,

<?php
$values = array(
    'foo' => 10,
    'bar' => 20,
    'baz' => 30
);

$newarray = array();
foreach($values as $k => $v) 
{
    $newarray[$k] = $v;
    if($k == 'bar') 
        $newarray['qux'] = 21;
}

echo implode(' ', $newarray);

Demo:
http://3v4l.org/N4XgB

演示:http:
//3v4l.org/N4XgB

回答by QHip

The solution below uses the same array.

下面的解决方案使用相同的数组。

$values = array(
    'foo' => 10,
    'bar' => 20,
    'baz' => 30
);

function match($niddle, $haystack, $push, $offset = 0) {
    foreach(array_slice($haystack, $offset) as $key => $value) 
    {
        print $value . ' ';
        if($key == $niddle) 
        {
            $i = array_search($niddle, array_keys($haystack)) + 1;
            $haystack = array_slice($haystack, 0, $i, true) + $push + array_slice($haystack, $i, count($haystack) - $i, true);
            $haystack = match($niddle, $haystack, $push, $i);
            break;
        }
    }

    return $haystack;
}

$values = match('bar', $values, array('qux'=>21));

var_dump($values);