"Undefined offset" Multidimensional Array Sum

Asked

Viewed 36 times

-1

My system reads the XML files inside a folder and puts the data ("invoice number", "product code","quantity") inside an array.

I created a new array to have the total per product, where the account is correct, and the result is what I expect.

The problem is that I get error below:

 Erro na Linha: #160 :: Undefined offset: 10452
C:\xampp\htdocs\controledeentregasver209112019\Romaneio\RomaneirodeCarregamentoTeste_1.php

Erro na Linha: #160 :: Undefined offset: 10485
C:\xampp\htdocs\controledeentregasver209112019\Romaneio\RomaneirodeCarregamentoTeste_1.php

Erro na Linha: #160 :: Undefined offset: 10433
C:\xampp\htdocs\controledeentregasver209112019\Romaneio\RomaneirodeCarregamentoTeste_1.php

Erro na Linha: #160 :: Undefined offset: 3767
C:\xampp\htdocs\controledeentregasver209112019\Romaneio\RomaneirodeCarregamentoTeste_1.php

Erro na Linha: #160 :: Undefined offset: 3768
C:\xampp\htdocs\controledeentregasver209112019\Romaneio\RomaneirodeCarregamentoTeste_1.php

C:\xampp\htdocs\controledeentregasver209112019\Romaneio\RomaneirodeCarregamentoTeste_1.php:171:
array (size=5)
  10452 => float 30
  10485 => float 20
  10433 => float 10
  3767 => float 40
  3768 => float 30

Follow the source code:

    <?php  

$notaPesoQuantidade = [];
$totalizando=[];

// ****************PEGANDO OS ARQUIVOS DA PASTA
$path   = "./arquivos/";
$files = scandir($path);
foreach ($files as &$value) {
    if($value=='.' ||$value=='..'):     
    else:
     $arquivo = simplexml_load_file('./arquivos/'."$value");


foreach($arquivo->NFe as $key => $xml){ 
//dados DA nf
$nNF = "".$xml->infNFe->ide->nNF.""  ; 
}
?> 



<?php
//IMPRIMINDO OS PRODUTOS DA NOTA FISCAL
echo "<br/>";

echo "<b><font color=\"#054F77\"> NF-e $nNF ($value) </font></b>";
?>
<table class="table">
  <thead>
    <tr>
      <th scope="col" style="color:#4169E1;">COD</th>

      <th scope="col"style="color:#4169E1;">Quant</th>

    </tr>
  </thead>
  <tbody>

<?php
//PERCORRENDO OS PRODUTOS DA NOTA FISCAL
 foreach ($xml->infNFe->det as $value) {
?>
    <tr>
      <th scope="row"><?php echo $value->prod->cProd; ?></th>
      <td style="font-weight: bold"><?php echo $value->prod->qCom; ?></td>



  <?php

  $nNF = intval ($nNF);
  $cProd = intval ($value->prod->cProd);
  $qCom= floatval ($value->prod->qCom);



//  $notaPesoQuantidade[$nNF]["$cProd"]=$qCom;

 $notaPesoQuantidade[] = [

         'nota'=>$nNF,
         'produto'=>$cProd,
         'quantidade'=>$qCom



                                 ];



?>       


    </tr>

<?php
}
?>
 </tbody>
</table>



<?php





echo "<hr>";
echo "<br>";  
echo "<br>";  



endif;




}



echo "<b><font color=\"#054F77\"> ***************** CARREGAMENTO TOTAL ******************</font></b>";



foreach ($notaPesoQuantidade as $key => $value) :
    //$key = indice do array
    //$value = é o arrai, ex $value['nota']
    $p = $value['produto'];
    $q = $value['quantidade'];


          $totalizando[$p]+=$q;





endforeach;




  var_dump($totalizando);



?>

1 answer

0


The solution I found was to change the logic. I first put everything in an array for products and then I disappeared.

$compras[$cProd][] = $qCom;

Summing up:

foreach ($compras as $key => $value) {
    echo "<br/>" . "$key = " . array_sum($value) . "<br/>";
}

Browser other questions tagged

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