Store amount of values in a PHP txt

Asked

Viewed 127 times

1

I have the information below in a file txt.

500038;204932;61312;FTE GAV EDIT LACAVA TOTAL LARGXALTX15 GAV. NORMAL;LACA BLU FOSCO#996#335;5;78101922;xxxxxxx;0204932005
500038;204932;61312;FTE GAV EDIT LACAVA TOTAL LARGXALTX15 GAV. NORMAL;LACA BLU FOSCO#696#166;12;xxxxxxx;0204932012
100398;204932;50384;CJTO OPC LAT DIR/ESQ ARMARIO 30XALTXPROF;MDF BP LEGGERO#1040#340;74;78728780;xxxxxxx;0204932074
100398;204932;50385;CJTO OPC BASE SUP/INF RETA ARM LARGX30XPROF;MDF BP LEGGERO#769#340;71;78728777;xxxxxxx;0204932071
100398;204932;61108;FTE GAV EDIT ALCA LARGXALTX18 GAV. QUAD;LEGGERO#696#164;77;78728783;xxxxxxx;0204932077
100398;204932;50057;PAI OPC EDIT LARGXALTX15;LEGGERO#2610#100;39;78728745;xxxxxxx;0204932039

I took this information and stored it in a array.

After that I took only the order number and stored in another array.

$file_name = "IMP.txt";

$file = fopen("ArquivoOrig/".$file_name, "r") or die ("Arquivo não encontrado!");
$file_output = fopen("ArquivoImp/Output_".$file_name, "w") or die ("Arquivo não criado!");

    while(!feof($file)){    
      $strAnalise =  fgets($file);
      $strAnaliseArray = explode(";", $strAnalise);
    if(trim($strAnaliseArray[7]) != ''){
        $ArrayChaveNome[$strAnaliseArray[1]] = trim($strAnaliseArray[7]);
    }
    $strArrayDados[] = $strAnaliseArray;
}

foreach ($strArrayDados as $key => $value) {
    if(trim($value[7]) == ''){
        if(array_key_exists($value[1], $ArrayChaveNome)){
            $strArrayDados[$key][7]= $ArrayChaveNome[$value[1]];
        }
    }
}

for ($i=0; $i < count($strArrayDados); $i++) { 
    $arrayTemp[$i] = $strArrayDados[$i][0];
}

var_dump($arrayValor);

//Monta arquivo
foreach ($strArrayDados as $key => $value) {
    $dataAtual = date('dmY');
    fwrite($file_output, $dataAtual.";");
    $resultPontoVirgulaToTxt = implode(";", $value);
    fwrite($file_output, $resultPontoVirgulaToTxt);

}

fclose($file_output);
fclose($file);

I used the function array_count_values to count order numbers, with 2 orders with the number 500038 and 4 applications under the number 100398.

$arrayValor = array_count_values($arrayTemp); 

Return:

Array
(
    [500038] => 2
    [100398] => 4
)

Now the question is, how do I place these values next to the order number dynamically, as the order numbers may vary and the quantity also. That’s what has to be in txt, do not print on screen.

How I need you to stay:

2;500038;204932;61312;FTE GAV EDIT LACAVA TOTAL LARGXALTX15 GAV. NORMAL;LACA BLU FOSCO#996#335;5;78101922;xxxxxxx;0204932005
2;500038;204932;61312;FTE GAV EDIT LACAVA TOTAL LARGXALTX15 GAV. NORMAL;LACA BLU FOSCO#696#166;12;xxxxxxx;0204932012
4;100398;204932;50384;CJTO OPC LAT DIR/ESQ ARMARIO 30XALTXPROF;MDF BP LEGGERO#1040#340;74;78728780;xxxxxxx;0204932074
4;100398;204932;50385;CJTO OPC BASE SUP/INF RETA ARM LARGX30XPROF;MDF BP LEGGERO#769#340;71;78728777;xxxxxxx;0204932071
4;100398;204932;61108;FTE GAV EDIT ALCA LARGXALTX18 GAV. QUAD;LEGGERO#696#164;77;78728783;xxxxxxx;0204932077
4;100398;204932;50057;PAI OPC EDIT LARGXALTX15;LEGGERO#2610#100;39;78728745;xxxxxxx;0204932039

1 answer

2


Its value came out a little different from what you quoted, here it appeared like this:

array(2) {
  ["500038;204932"]=>
  int(2)
  ["100398;204932"]=>
  int(4)
}

I think there are several ways to do this, one that you can use is the explode as a limiter, or a simple str_replace:

<?php
$valoresStr = '500038;204932
500038;204932
100398;204932
100398;204932
100398;204932
100398;204932';

$arrayTemp = explode(chr(10), trim($valoresStr));

$arrayValores = array_count_values($arrayTemp);

var_dump($arrayValores);

foreach ($arrayValores as $valor => $soma) {
    $valoresStr = str_replace($valor, $soma . ';' . $valor, $valoresStr);
}

echo PHP_EOL;

echo $valoresStr;

See what I changed $valor for $soma . ';' . $valor, that is to say add the prefix as the sum value, of course this will work for your example, now I do not know if there will be variations after the ; of this 500038;204932, for example this 500038;2000001

Online example: http://ideone.com/wDGpXc

  • Before storing I already gave one explode on boundaries which are ;. I forgot to mention it. That’s why it came out like this Array&#xA;(&#xA; [500038] => 2&#xA; [100398] => 4&#xA;)

  • @Kevin. F then puts the code in the question so that we can understand, please

  • His answer did not give the answer to me but I managed to do, his answer cleared things up.

  • @Kevin. F did not work because the example in your question is incomplete, if put complete I will be able to formulate it the way you need. Your updated example keeps coming out different

  • So I put all my code and all the information you have on txt see if you’re better now to understand.

  • @Kevin. F is now better, I’ll try to adjust the example.

Show 1 more comment

Browser other questions tagged

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