Add input to an array loop

Asked

Viewed 45 times

-4

for($i=0; $i<count($produtos); $i++) {
            
            $itens = array(); 

            for($j=0; $j<count($especificacao); $j++) {

                if ($produtos[$i]["produto_id"] === $especificacao[$j]["prod_espeficacao_produto_id"]) {
                    array_push($itens, $especificacao[$j]);
                }          
            }

           array_push($produtos[$i], $itens);
        }

I would like to add an example index "test" in this place marked at the time of the array_push would be possible ??

1 answer

2


In fact, by setting the [$i] in the push array, you are already assigning a key/index to the products array.

Ex: In the first run of for, where $i = 0, when executing the [$products[$i], $items); you are assigning the index 0 to your array, that is, to add the test index, just run the arra_push this way:

array_push($products['test'], $items).

if you prefer, you can add this way too:

$products['test'] = $items;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.