Php. Number of zeros in float - php

I have to send the JSON:
{"val": 5000.00}
Neither {"val": "5000.00"} nor {"val": 5000} is correct format.
json_encode() converts 5000.00 to 5000
Is it possible to send correct json's format (two zeros) with json_encode from
array("val" => (float) 5000.00) ?

No, it's not possible, because what you think are "correct" and "incorrect" values are actually the same value. It's purely a rendering decision to show/hide trailing zeros.
It's up to you at display time to render the value with the correct number of decimal places. You can't force a floating point number to be stored or transfered with a certain number of decimals.

If you really really need to do that you may consider two options:
Option nr. 1:
You build up the json econded string by your own. If the data you have to encode have a simple structure and that structure is not subject to change in the future the task is easy. You'll have a slower script so another requirement is that the data is not too much.
For a single object like the example you posted...
$json = sprintf('{"val": %.2f}', $floatValue);
Of course for structured data, like an array of objects, arrays of arrays... you'll have to write the necessary loops, place accurately , : [ ] { } ". Hope I gave you the idea...
Option nr. 2:
You build up the data you have to encode just as you would if you didn't have the weird requirement you asked for. But store float values into strings that will also contains some pattern characters.
For example you'll store 5000.00 as "##5000.00##"
Encode the data with json_ecode.
Permorm string replacements to eliminate "## and ##"
$floatValue = 5000.00;
$data = array("val" => sprintf("##%.2f##", $floatValue));
$json = json_encode($data);
$json = str_replace('"##', '', $json);
$json = str_replace('##"', '', $json);
Choose your pattern characters (## in the example) carefully to avoid conflicts with other strings that may contain the same pattern elsewere in you data to be encoded.

just preg_replace('/:"(\d+\.\d+)",/', ':$1,', json_encode($a, JSON_FORCE_OBJECT))

Related

json_encode() converts large number to scientific notation [duplicate]

As noted in the PHP documentation, when json_decodeing a data structure containing long integers, they'll be converted to floats. The workaround is to use JSON_BIGINT_AS_STRING, which preserves them as strings instead. When json_encodeing such values, JSON_NUMERIC_CHECK will encode those numbers back into large integers:
$json = '{"foo":283675428357628352}';
$obj = json_decode($json, false, JSON_BIGINT_AS_STRING);
$json2 = json_encode($obj, JSON_NUMERIC_CHECK);
var_dump($json === $json2); // true
Using this method for a correct roundtrip of the data is prone to errors. If a property contains '123', a numeric string which should stay a string, it will be encoded to an integer.
I want to get an object from the server, modify one property and than put the entire data structure back. I need to preserve the original types. I don't want to maintain properties other than the one I'm manipulating.
Is there any real workaround for this? PHP does not have any issues with big ints anymore, but the json_decode routine seems to be outdated.
As long as your PHP version can actually handle large integers, meaning if you're running a 64-bit version of PHP (on something other than Windows), json_decode has no problem with it:
$json = '{"foo":9223372036854775807}';
$obj = json_decode($json);
$json2 = json_encode($obj);
var_dump(PHP_INT_MAX, $obj, $json2);
int(9223372036854775807)
object(stdClass)#1 (1) {
["foo"]=>
int(9223372036854775807)
}
string(27) "{"foo":9223372036854775807}"
If the integer values you need to handle do exceed PHP's PHP_INT_MAX, you simply cannot represent them in PHP native types. So there's no way around the conundrum you have; you cannot use native types to track the correct type, and you cannot substitute other types (e.g. strings instead of integers), because that's ambiguous when encoding back to JSON.
In this case you will have to invent your own mechanism of tracking the correct types for each property and handle such serialisation with a custom encoder/decoder. For example, you'd need to write a custom JSON decoder which can decode to a custom class like new JsonInteger('9223372036854775808'), and your custom encoder would recognise this type and encode it to a JSON 9223372036854775808 value.
There's no such thing built into PHP.
For what it's worth, PHP can support values > PHP_INT_MAX using the bcmath package http://php.net/manual/en/book.bc.php but JSON is a slightly more difficult issue.
To answer the OP's question of why they can't encode the value from a string back to an int type in the JSON, the answer lies in the conversion step. When reading the original JSON string in, it's a string, and read byte by byte. When reading values, they're initially read as a string (as the JSON itself if a string), and later cast to the correct type to an int or a float depending upon the presence of a period (.). If the value is greater than PHP_INT_MAX then PHP converts it to a double, and you lose precision. Thus using JSON_BIGINT_AS_STRING will tell the parser to keep the value as a string and NOT try to cast it, everything should be good, the value is kept in tact, albeit a string.
The problem comes when doing the inverse, and doing json_encode($value, JSON_NUMERIC_CHECK) tells PHP to cast string numeric values into either int/float, but this appears to happen BEFORE writing to the JSON string, causing values > PHP_INT_MAX to be converted into a double representation like 9.2233720368548e+19
See https://3v4l.org/lHL62 or below:
$bigger_than_max = '{"max": ' . PHP_INT_MAX . '1}'; // appending 1 makes > PHP_INT_MAX
var_dump($bigger_than_max);
var_dump(json_decode($bigger_than_max));
var_dump(json_decode($bigger_than_max, false, 512, JSON_BIGINT_AS_STRING));
var_dump(json_encode(json_decode($bigger_than_max, false, 512, JSON_BIGINT_AS_STRING)));
var_dump(json_encode(json_decode($bigger_than_max, false, 512, JSON_BIGINT_AS_STRING), JSON_NUMERIC_CHECK));
Result:
string(29) "{"max": 92233720368547758071}"
object(stdClass)#1 (1) {
["max"]=>
float(9.2233720368548E+19)
}
object(stdClass)#1 (1) {
["max"]=>
string(20) "92233720368547758071"
}
string(30) "{"max":"92233720368547758071"}"
string(29) "{"max":9.223372036854776e+19}"
Unfortunately, it doesn't appear that there is a way to solve this, looking at the JSON constants http://php.net/manual/en/json.constants.php I don't see anything that allows you to write integer values > PHP_INT_MAX into ints within the JSON.
Sorry this doesn't find a solution but hopefully clears up some confusion.

php serialize huge float causes rounding and formatting issues

I have the following php snippet
$newData = serialize(array('ep' => 50733372961735.4));
echo "New data: " . print_r($newData, 1);
Output:
New data: a:1:{s:2:"ep";d:5.07333729617E+13;}
But I would like the float value as it is and not E+13.
What could I do without having to make drastic changes as this is just an example. In my actual code the 'ep' value could be inside a complex array hierarchy
Firstly, a general note: serialize should never be used on data that could be manipulated in any way. It's useful for things like session data and caches, but should not be relied on for transporting data between applications or data storage. In many cases, you're better off using a standard serialization format like JSON.
You also certainly shouldn't care what the serialized string looks like - the only thing you should do with that string is pass it back to unserialize(). So the fact that there is E+13 is not a problem if the actual value it gives back when you unserialize is the one you wanted.
However, it's clear in your example that you have lost precision - the last digits are ...29617 rather than ...29617354 - so back to the point: there is a PHP setting serialize_precision, described in the manual here. It's default value has varied over the years, but setting it to an explicit value other than -1 will serialize floats with that number of significant figures:
ini_set('serialize_precision', 2);
echo serialize(50733372961735.4), PHP_EOL;
// d:5.1E+13;
ini_set('serialize_precision', 20);
echo serialize(50733372961735.4), PHP_EOL;
// d:50733372961735.398438;
Note that the first example has clearly thrown away information, whereas the second has actually stored more precision than you realised you had - because of the inaccuracy of storing decimals in binary floating point format.
The problem is not the serialize at all!
it's numbers in general.
$num = 50733372961735.4;
print($num);
=> 50733372961735
to "solve" this problem you can use:
ini_set('serialize_precision', 15);
I just executed your code on www.writephponline.com
I got the below value if I did not put your value in string :- $newData = serialize(array('ep' => 50733372961735.4));
New data: a:1:{s:2:"ep";d:50733372961735.398;}
and this after adding it in string :- $newData = serialize(array('ep' => '50733372961735.4'));
New data: a:1:{s:2:"ep";s:16:"50733372961735.4";}

php correct json numbers and strings encoding

I have a problem with encoding php array to json. Part of my data are stings like '555', '3M', part of data are numbers: floats or integers. On frontend I need to receive strings as strings and numbers as numbers as numbers for correct sorting, searching , etc. Php has a JSON_NUMERIC_CHECK, but this will convert my sting '555' value into number. Is there any approach except first converting strings like strings, then numbers with JSON_NUMERIC_CHECK option and then concatenating result?
$a = json_encode(['555', '3M', 123, 1.2, 1.3]);
var_dump($a);//outputs string(24) "["555","3M",123,1.2,1.3]"
Maybe you send data the wrong way. Try to see what you receive after your AJAX (POST method). Or read how correctly send data with AJAX.
http://api.jquery.com/jquery.ajax/
Actually, the problem was with numbers. Because I queried numeric data from db, I received it as strings. So I converted string to numbers, and received correctly encoded data on frontend:
array_walk_recursive($data, function(&$val) {
if (is_numeric($val)) $val = (float) $val;
});

Are there any binary serializers available?

Going for space savings here. I have an object that I need to serialize for each row.
In theory, it needs to consist of 168 16b unsigned integers (even though there's no explicit typing in php), that should be worth 336 bytes.
If I go for the serialize() function that creates strings though, the size is up to 2349 bytes.
Is there a binary kind of serializer for php?
Store the data in a plain and simple array. Then pack it:
$len = count($arr);
$data = pack("S$len", ...$arr); // requires PHP5.6+
To get the array back you want to unpack()it:
$arr = unpack("S$len", $data);
Mind you that you need to keep the $len variable along the $data block.
More about it here.

Serialize an array and compress the string to use it in a url

The problem:
We need to serilize an array into a short string, the shorter the better.
The importance is more reflected on the bigger array than the smaller one.
The string will be used in a get request so it has to be url decoded.
Current code snippet
/*
array (size=3)
0 => string '/js/dhdbm78hdfb.js' (length=18)
1 => string '/js/dfg4dg.js' (length=13)
2 => string '/js/fg8hfhrt.js' (length=15)
2 => string '/js/yjtdygj.js' (length=14)
2 => string '/js/2q5g54rvfd.js' (length=17)
*/
$json = json_encode($data);
$gz = gzdeflate($json, 9);
$base64 = base64_encode($gz);
$paths = urlencode($base64);
// outputs: i1aK0c8qjtFPyUhJyjW3yEhJS9LLKlbSgQmnpZukpCOLpKVbZKRlFJUgi1VmlaRUpmchCxkVmqabmhSVpaWARGMB
Not very impressive and pretty slow, I think there should be a better way of doing this...
Question
What would be the best approach to this problem? How can we present the smallest string possible?
PS
It's not the biggest issue if it's slow but it's a variable to be considered. The array will be hashed and retrived from cache when possible.
If you want it as fast and as small as possible, ditch the general purpose tools and use a method suited to the particular data you're sending. Strip out anything duplicated between all of the array values (e.g. "/js/" and ".js"), strip out all of the array syntax from serialization, and just send a string concatenated list of your unique values, and reconstitute it on the receiving end.
Further compress it with gz and base64_encode if you must, but one of the drawbacks of having unique data is that it must be uniquely represented.
See the selected answer here: How to compress/decompress a long query string in PHP?
Edit:
do you have the leeway to POST the data instead? That will at least avoid the direct size restrictions associated with the querystring. It'll also avoid the necessity to encode the string for the URL, adding length, and you should be able to send the zipped content directly, for most savings.
serialization = json_encode
compression = gzdeflate
urlsafe = base64_encode
urlencode isn't needed as base64_encode produces safe characters only. I'd keep the base64_encode in there. You could consider finding a faster serializer or a faster compressor. You could even leave out the compressor if your input is short enough.
Algorithms have performance characteristics in two dimensions: number of instructions and memory usage. Typically you'll find that if you try to reduce one the other goes up. Indexes in a database allow for fast finding but take up a lot of space. Index-less finding is slow but doesn't require extra space for indexes.
Since you want short that means you need to make sacrifices in terms of fast.
Honestly, I think you've got a pretty optimal set here.
Try to use gzcompress() function
http://php.net/manual/en/function.gzcompress.php

Categories