Search code examples
phparraysstringnumericnotice

On assigning array: "PHP Notice: A non well formed numeric value encountered"


I get this error:

PHP Notice: A non well formed numeric value encountered in [...]/model.php on line 205

In this PHP code:

   [...]

   echo 'var_dump($i):' . "\n";
   var_dump($i);

   echo 'var_dump($wawiData[$i][\'date\']):' . "\n";
   var_dump($wawiData[$i]['date']);

   $accRecords[] = array(
    'date' => $wawiData[$i]['date'],  # this is line 205
    'description' => $wawiData[$i]['inv-recipient'],
    'field1' => $wawiData[$i]['inv-no'],
    'account' => $account,
    'contra-account' => $wawiData[$i]['client-no'],
    'amount' => $wawiData[$i]['amount'] * -1,  # this is line 210
    'country-code' => $country,
    'rec-type' => $wawiData[$i]['rec-type'],
    'company' => $wawiData[$i]['company'],
    'name' => $wawiData[$i]['name'],
    'freeze' => 0,
    'inv-link' => 'BEDI "' . $guids[$wawiData[$i]['inv-no']] . '"'
   );
  }
 }
 return $accRecords;
}  # end of method

Output on console:

var_dump($i):
int(0)
var_dump($wawiData[$i]['date']):
string(10) "08.01.2018"
PHP Notice:  A non well formed numeric value encountered in [...]/model.php on line 205

I found lots of posts about this PHP Notice, but all were about code using date() and other functions with malformed parameters.

However, I can't see how this relates to me simply assigning a perfectly fine string to an array using a perfectly fine int as index.

Can anyone explain to me what causes the PHP Notice? (line 205 is marked in the code above)

Thanks!

P.S.

var_dump($wawiData[$i]):
array(10) {
  ["inv-no"]=>
  string(11) "RE2018-6677"
  ["date"]=>
  string(10) "08.01.2018"
  ["amount"]=>
  string(5) "93,79"
  ["client-no"]=>
  string(5) "12881"
  ["inv-recipient"]=>
  string(27) "(...)"
  ["company"]=>
  string(27) "(...)"
  ["name"]=>
  string(13) "(...)"
  ["rec-type"]=>
  int(2)
  ["country"]=>
  string(11) "Deutschland"
  ["country-code"]=>
  string(2) "DE"
}

Solution

  • Comment by MacBooc:
    $wawiData[$i]['amount'] * -1 with 93,79 couldn't be the issue? try to replace your , by ., i'm pretty sure your error will leave

    Kudos to @MacBooc for this answer!

    Elaboration:

    The PHP Notice is triggered because I tried to multiply the value 93,79 by -1. This causes the notice, as PHP expects a . (period) as decimal separator and therefore does not like the , (comma - a.k.a. German decimal separator).

    Additional confusion was caused by the fact, that PHP claimed the notice was caused in line 205, though is was actually caused in line 210.

    I added the following lines before line 204:

    # remove possible thousands separators
    $amount = str_replace('.', '', $wawiData[$i]['amount']);
    # change decimal separator from ',' to '.' and mulitply by -1
    $amount = str_replace(',', '.', $amount) * -1;
    # change decimal separator back from '.' to ','
    $amount = str_replace('.', ',', $amount);
    

    My new code for line 210:

    'amount' => $amount,