1

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

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.

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

2 Answers 2

2

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

foreach ($newItems as $newId => $n) {
    $newItems[$newId]['qty'] = 1;
    $newItems[$newId]['deal_id'] = $newId;
}
Sign up to request clarification or add additional context in comments.

Comments

2

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

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

See the docs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.