Increment an array with PHP foreach

Asked

Viewed 52 times

0

I have a foreach that where I need to add the value of the variable to a array:

foreach ($xml->NFe->infNFe->det as $itens){    
    $quantidade = round($itens->prod->qCom);
    $total_quant_unidade = array();
    array_push($total_quant_unidade, $quantidade);
}

The problem is that only the ultimate value is added even within the foreach.

1 answer

2


The total_quant_unit variable must be outside the foreach

$total_quant_unidade = array();

foreach ($xml->NFe->infNFe->det as $itens){    
    $quantidade = round($itens->prod->qCom);
    array_push($total_quant_unidade, $quantidade);
}
  • it was stupid of mine I was creating the array every time the foreach passed , thank you very much

Browser other questions tagged

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