无法修改数组元素值

时间:2021-03-21 20:20:09

I have some code that loops through an array, and, if an element of it has a qty of more than 1, copies that element out into as many elements as the qty and then sets the qty of all of them to 1 (and makes a few other changes.)

我有一些循环遍历数组的代码,如果它的元素的数量大于1,则将该元素复制到与qty一样多的元素中,然后将所有元素的数量设置为1(并使得其他一些变化。)

I've been staring at this for a long time, and I'm sure I must be missing something obvious, because I cannot for the life of me get it to change the values I want to change.

我已经盯着这个很长一段时间了,我相信我一定会错过一些明显的东西,因为我不能为我的生活让它改变我想要改变的价值观。

Here's the basic code:

这是基本代码:

foreach ($items as $id => $itm) {
    // if there's an item that has a qty > 1, need to split out into multiple nodes with qty = 1
    if ($itm['qty'] > 1) {
        $qtyCount = $itm['qty'];
        $newItems = array_fill($id, $qtyCount, $itm);

        foreach ($newItems as $newId => $n) {
            $n['qty'] = 1;
            $n['deal_id'] = $newId;
        }

        error_log("new items");
        error_log(print_r($newItems,1));
    }
}

What I would start with is an array that looks like this:

我开始的是一个看起来像这样的数组:

[items] => Array
    (
        [0] => Array
            (
                [product] => AW8B
                [qty] => 1
                [subtotal] => 6.500
                [deal_id] => 
            )

        [1] => Array
            (
                [product] => C
                [qty] => 3
                [subtotal] => 30.000
                [deal_id] => 1
            )

        [2] => Array
            (
                [product] => QUEPAP
                [qty] => 1
                [subtotal] => 4.000
                [deal_id] => 
            )

    )

In this case, only element 1 in the original items array meets the criteria of qty > 1, so the code should create a new array containing three identical elements to start with. Then the foreach($newItems as $newId => $n) loop should loop through the new, 3-element array, and set the qty element for each one to 1 and the deal_id element to the loop index. What I would expect the error_log(print_r($newItems,1)) line to return is something that looks like the following.

在这种情况下,只有原始项数组中的元素1符合qty> 1的条件,因此代码应该创建一个包含三个相同元素的新数组。然后foreach($ newItems as $ newId => $ n)循环应循环遍历新的3元素数组,并将每个元素的qty元素设置为1,将deal_id元素设置为循环索引。我期望返回的error_log(print_r($ newItems,1))行看起来如下所示。

[newItems] => Array
(
    [1] => Array 
        (
            [product] => C
            [qty] => 1
            [subtotal] => 30.000
            [deal_id] => 1
        )

    [2] => Array
        (
            [product] => C
            [qty] => 1
            [subtotal] => 30.000
            [deal_id] => 2
        )

    [3] => Array
        (
            [product] => C
            [qty] => 1
            [subtotal] => 30.000
            [deal_id] => 3
        )
) 

However, the qty remains 3 and the deal_id remains 1 for all of them, even after the internal foreach loop has run. This is driving me up a wall, because I'm not a newbie and have done this kind of thing before. Just cannot for the life of me figure out what I'm doing wrong here.

但是,即使在内部foreach循环运行之后,qty仍然为3,并且deal_id对所有这些都保持为1。这让我起了墙,因为我不是新手,而且以前做过这种事。只是不能为我的生活弄清楚我在这里做错了什么。

2 个解决方案

#1


1  

I would do it as Leonardo shows, but an alternate:

我会像莱昂纳多所说的那样做,但是另一个:

foreach ($newItems as $newId => $n) {
    $newItems[$newId]['qty'] = 1;
    $newItems[$newId]['deal_id'] = $newId;
}

#2


1  

To modify an array directly in a foreach loop you must use '&', like this:

要在foreach循环中直接修改数组,必须使用'&',如下所示:

foreach ($newItems as $newId => &$n) {
    ....
}

See the docs

查看文档

#1


1  

I would do it as Leonardo shows, but an alternate:

我会像莱昂纳多所说的那样做,但是另一个:

foreach ($newItems as $newId => $n) {
    $newItems[$newId]['qty'] = 1;
    $newItems[$newId]['deal_id'] = $newId;
}

#2


1  

To modify an array directly in a foreach loop you must use '&', like this:

要在foreach循环中直接修改数组,必须使用'&',如下所示:

foreach ($newItems as $newId => &$n) {
    ....
}

See the docs

查看文档