Store number of values in a PHP txt

1

I have the following information in a txt file.

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 array .

After that, I just took the number of the orders and stored it 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 array_count_values function to count the order numbers, with 2 requests with the number 500038 and 4 requests with 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, because the order numbers can vary and the quantity too. This is what has to be in the txt , does not print on the screen.

How do I 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
    
asked by anonymous 11.11.2016 / 14:41

1 answer

2

Your value came out a bit 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 you can use is 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 that I changed $valor by $soma . ';' . $valor , that means that I add the prefix as the sum value, of course this will work for your example, now I can not tell if there will be variations after ; of this 500038;204932 , for example this 500038;2000001

Example online: link

    
11.11.2016 / 14:57