0

I'm inserting elements dinamically in array, elements are inserted OK, but only the last one

I think that push_array will solve the problem, and I try

array_push($rowsItemAddit, $rowsItemAddit["item"]["acId"], $precio_row_addit->acId);

But I get error

Notice: Undefined index: item

My code working for the last element is

foreach($precios_row_addit as $precio_row_addit){
    $rowsItemAddit["item"]["acId"] = $precio_row_addit->acId;
    $rowsItemAddit["item"]["acValues"]  = array ( 'acValue' => $precio_row_addit->acValue );                    
    }

Any ideas to get the complete array?

The final structure should be:

[additionalColumnValues] => Array
    (
        [item] => Array
            (
                [acId] => 0
                [acValues] => Array
                    (
                        [acValue] => 10
                    )

            )
        [item] => Array
            (
                [acId] => 1
                [acValues] => Array
                    (
                        [acValue] => 10
                    )

            )               
        [item] => Array
            (
                [acId] => etc.
                [acValues] => Array
                    (
                        [acValue] => 10
                    )

            )
    )

        )

)

TIA

2 Answers 2

1

You need to code it like this

foreach ($precios_row_addit as $precio_row_addit) {
    $rowsItemAddit[] = array('item' => array(
            'acId' => $precio_row_addit->acId,
            'acValues' => array('acValue' => $precio_row_addit->acValue)
        ));
}
Sign up to request clarification or add additional context in comments.

1 Comment

This would not match the structure he wanted
0

You always try to overwrite the same array elements. It can't have multiple keys named "item"

Try something like this, where the item index is an numeric index rather than "item":

foreach($precios_row_addit as $precio_row_addit){
    $rowsItemAddit[]["acId"] = $precio_row_addit->acId;
    $rowsItemAddit[]["acValues"] = array ( 'acValue' => $precio_row_addit->acValue );                    
}

8 Comments

Not exactly what I wanted. Here my results
Array([0] => Array ( [acId] => 0 ) [1] => Array ( [acValues] => Array ( [acValue] => 10 ) ) [2] => Array ( [acId] => 1 ) [3] => Array ( [acValues] => Array ( [acValue] => K-CostCenter ) ) )
The problem with $rowsItemAddit[] is that now there are 2 different nodes,
Hmm.. yeah. But now you know, that the structure you wanted to have can not work with arrays in php, right? An array key is a unique identifier which can not appear twice or more
Thanks all for your answers, finally I've got it guided with your thoughts. This is the final code, I use a temp array $wrk = array(); $wrk ["acId"] = $precio_row_addit->acId; $wrk ["acValues"]["acValue"] = $precio_row_addit->acValue; $rowsItemAddit["item"] = $wrk;
|

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.