I have a PHP function that gets data from the database and returns an array with the results. This array by default has all the data as a string , however some fields should be treated as boolean while others should be treated as values (money) and are of type decimal .
Later I make use of this data in the HTML page to perform some functions or data display. For example:
-
valor
- type _decimal (10,2) - Used to display the value of a product. -
ativo
- type tinyint - Used to mark or not a checkbox.
The array of data I get is generated through a query (SELECT) to the database, so the return comes with all fields in string
. This "solves" a part of the problem, since it keeps the valor
field to the decimal places. The problem starts when I try to convert, using flags in json_encode
, transform values back to boolean (or int ), and so I have these 2 cases:
Case 1: When I use json_encode($resposta,JSON_NUMERIC_CHECK)
the column valor
loses its decimals and is rounded. Ex: 9.90 (string) - > 10 (int)
Case 2: When I use json_encode($resposta,JSON_PRESERVE_ZERO_FRACTION )
the column ativo
comes as a string and therefore I can not control the checkbox .
Is there any way around this?