4
I have a PHP function that takes data from the database and returns an array with the results. This array by default has all the data as type string, but some fields should be treated as Boolean while others should be treated as values (money) and are of the type decimal.
Later I make use of this data in the HTML page to perform some functions or display data. For example:
valor
- type _decimal(10,2) - Used to display the value of a product.ativo
- tinyint type - Used to mark or not a checkbox.
The array of data I obtain is generated through a query (SELECT) to the database, with this the return comes with all fields in string
. This "solves" a part of the problem as it keeps the field valor
with the decimal places. The problem starts when I try the conversion, using flags in the json_encode
, turn the values back to Boolean (or int), and then I have these two 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 string and I can’t control the checkbox.
Is there any way around this situation?
Strange I just tested, http://ideone.com/EPzTTv did not lose the decimals, and did not put extra parameters on
json_encode
– Miguel
@Miguel but here you are generating an array manually, what I do is an sql request to the database, where the initial values returned are all in string. Which solves the case of decimal values, because it keeps the houses. However I need to convert in order to use the values of type Boolean, it is in this conversion that I lose the decimals.
– celsomtrindade
On "active" why use tinyint instead of "Enum"?
– Danilo Miguel
@Danilomiguel, I don’t think this will interfere with the outcome, it would just be a structural change. By the way, several questions I read in the English OS do not even recommend Enum, but tinyint or bit. As the bank already has this structure, I preferred to keep it. But I think that is not the case.
– celsomtrindade
You can post a snippet of code and a var_dump() of the return, including after json_encode?
– Danilo Miguel
abs()
or(bool)
in the columnativo
Before sending json_encode would not solve in the second case? JSON_NUMERIC_CHECK is transforming its strings into ints but if vc transform before JSON_PRESERVE_ZERO_FRACTION should not interfere with this.– Ricardo Moraleida
Celsom, see this possible solution, http://ideone.com/EPzTTv , here converts the value manually to float and the asset to int
– Miguel
@Richerdomoraleida you get a more practical example of how I could do it?
– celsomtrindade
@Miguel but in this case, if there is a json_encode then would not "reset" and convert back to string? Because I thought about using a foreach as an alternative, but I think the problem is happening later, when using json_encode. As I use the javascript data later, this step is required.
– celsomtrindade
@Celsomtrindade something like this, I didn’t optimize: https://eval.in/673597
– Ricardo Moraleida
@That’s what Ricardomoraleida said! That’s the way it is. The only difference is that instead of using JSON_NUMERIC_CHECK I had to use JSON_PRESERVE_ZERO_FRACTION so it returned me value(string) and active(int) as I needed. A question about this.. Is there a method to change the value field instead of the asset? ex.:
string($produto['valor'])
. So I can keep the id (or other int fields) as int as well and only the value as string.– celsomtrindade