Error reading CSV: A non-formed numeric value encountered

0

I'm trying to get 3 different values from 3 distinct csv files and multiply them, but it's giving this error. Where did I go wrong? Or what is missing add?!

Error:

  

Notice: A non-formed numeric value encountered in C: \ xampp \ htdocs \ Accessing CSV files \ fgetcsv.php on line 34

     

Notice: A non-formed numeric value encountered in C: \ xampp \ htdocs \ Accessing CSV files \ fgetcsv.php on line 34

Code:

<?php
$file1 = __DIR__ . '/Trabalhos.csv';
$csv1 = file($file1);
foreach ($csv1 as $row1 => $line1) {
    $row1++;
    $column1 = str_getcsv($line1, ';');
    if ($row1 == 2) {
        echo $column1[6]."<br>";
        $valor1 = $column1[6];
    }
}

$file2 = __DIR__ . '/produtividade do trabalho.csv';
$csv2 = file($file2);
foreach ($csv2 as $row2 => $line2) {
    $row2++;
    $column2 = str_getcsv($line2, ';');
    if ($row2 == 2) {
        echo $column2[9]."<br>";
        $valor2 = $column2[9];
    }
}
$file3 = __DIR__ . '/inatividade do trabalho.csv';
$csv3 = file($file3);
foreach ($csv3 as $row3 => $line3) {
    $row3++;
    $column3 = str_getcsv($line3, ';');
    if ($row3 == 2) {
        echo $column3[0]."<br>";
        $valor3 = $column3[0];
    }
}

$total = $valor1 * $valor2 * $valor3

?>
    
asked by anonymous 21.08.2017 / 23:55

1 answer

1

The problem is in the contents of one of the CSVs, there is no way to know, but the problem is that by picking the value as in:

 echo $column2[9]."<br>";

It should be getting the wrong item in one of them, then when trying to multiply:

 $total = $valor1 * $valor2 * $valor3;

It should be causing the error, see if you do this you will get the error:

<?php

$valor1 = 'foo';
$valor2 = '2';
$valor3 = '3';

$total = $valor1 * $valor2 * $valor3;

In other words, the message indicates that you are trying to perform a mathematical operation without having a valid number:

  

Notice: A well-formed numeric value encountered

It's important to understand that formats with , in numbers are not considered numbers for math operations, so if you get something like:

  

1,074.00

This will not be a "valid" number, so just try, follow the revised code (to simplify and avoid repeating movi codes for a function)

<?php

function pegar_valor_coluna($arquivo, $getrow, $getcol)
{
    $csv = file($arquivo);
    $row = 0;

    foreach ($csv as $row => $line) {
        $row++;

        if ($row == $getrow) {
            $column = str_getcsv($line, ';');
            $valor = $column[$getcol];

            //Conversão para números simples

            //Conversão para números simples
            $valor = str_replace(array('.', ' '), '', $valor);
            $valor = str_replace(',', '', $valor);

            return $valor;
        }
    }
}

$valor1 = pegar_valor_coluna(__DIR__ . '/Trabalhos.csv', 2, 6);
$valor2 = pegar_valor_coluna(__DIR__ . '/produtividade do trabalho.csv', 2, 9);
$valor3 = pegar_valor_coluna(__DIR__ . '/inatividade do trabalho.csv', 2, 0);

$total = $valor1 * $valor2 * $valor3;
    
22.08.2017 / 00:09