Error reading CSV: A non well Formed Numeric value encountered

Asked

Viewed 2,240 times

1

I am trying to take 3 different values from 3 distinct csv file and multiply them, but it is giving this error. Where did I go wrong? Or what’s left to add?!

Error:

Notice: A non well Formed Numeric value encountered in C: xampp htdocs Accessing CSV fgetcsv.php files on line 34

Notice: A non well Formed Numeric value encountered in C: xampp htdocs Accessing CSV fgetcsv.php files 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

?>

1 answer

1


The problem is in the content of one of the Csvs, there is no way to know, but the problem is that by taking the value as in:

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

You must be picking the wrong item on one of them, so when trying to multiply:

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

You must 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 do a mathematical operation without having a valid number:

Notice: A non well Formed Numeric value encountered

It is important to understand what formats with , numbers are not considered numbers for mathematical operations, so if you receive something like:

1.074,00

This will not be a "valid" number, so just treat, follow the revised code (to simplify and avoid repetition of 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;
  • Yes, it made the same mistake. I will move the "foo" to "1" and did the multiplication perfectly. So, what should I do?! Because it is these values 1.074,00 60,00 2091832 , that he takes from the csv files. Is not accepting because of the commas and dots?!

  • Whoa, wait. The error that gives is this one: Warning: A non-numeric value encountered in, it’s not the same. Sorry.

  • @Alex_alex_alex in HOST values like spaces and commas nay are numeric values, for mathematical operations only INT and Float are functional.

  • Then I will turn the values into float, got it. It will solve my problem. Thanks William!

  • @Alex_alex_alex I added an example to convert to number in the answer, please test it had a bug in the previous example

Browser other questions tagged

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