NotionCommotion Posted December 27, 2016 Share Posted December 27, 2016 (edited) b[0]->c is 128. How can I make json_decode return it as a float and not an integer? The only way that I am able to do so is hardcode 128 as either 128. or 128.0, but I don't wish to hard code it. Thanks <?php $json='{"a": 123, "b": [{"c":127.2},{"c":128},{"c":129.2}], "d":123.321}'; echo '<pre>';var_dump(json_decode($json));echo '</pre>'; $json='{"a": 123, "b": [{"c":127.2},{"c":128.0},{"c":129.2}], "d":123.321}'; echo '<pre>';var_dump(json_decode($json));echo '</pre>'; $json='{"a": 123, "b": [{"c":127.2},{"c":128.},{"c":129.2}], "d":123.321}'; echo '<pre>';var_dump(json_decode($json));echo '</pre>'; function display($c1) { $arr=["a"=>123,"b"=>[["c"=>127.2],["c"=>$c1],["c"=>129.2]],"d"=>123.321]; $json=json_encode($arr); echo '<pre>';var_dump(json_decode($json));echo '</pre>'; } $c1=128; display($c1); $c1=(float) $c1; display($c1); $c1=128.0; display($c1); object(stdClass)#1 (3) { ["a"]=> int(123) ["b"]=> array(3) { [0]=> object(stdClass)#2 (1) { ["c"]=> float(127.2) } [1]=> object(stdClass)#3 (1) { ["c"]=> int(128) } [2]=> object(stdClass)#4 (1) { ["c"]=> float(129.2) } } ["d"]=> float(123.321) } object(stdClass)#1 (3) { ["a"]=> int(123) ["b"]=> array(3) { [0]=> object(stdClass)#4 (1) { ["c"]=> float(127.2) } [1]=> object(stdClass)#3 (1) { ["c"]=> float(128) } [2]=> object(stdClass)#2 (1) { ["c"]=> float(129.2) } } ["d"]=> float(123.321) } object(stdClass)#1 (3) { ["a"]=> int(123) ["b"]=> array(3) { [0]=> object(stdClass)#2 (1) { ["c"]=> float(127.2) } [1]=> object(stdClass)#3 (1) { ["c"]=> float(128) } [2]=> object(stdClass)#4 (1) { ["c"]=> float(129.2) } } ["d"]=> float(123.321) } object(stdClass)#1 (3) { ["a"]=> int(123) ["b"]=> array(3) { [0]=> object(stdClass)#4 (1) { ["c"]=> float(127.2) } [1]=> object(stdClass)#3 (1) { ["c"]=> int(128) } [2]=> object(stdClass)#2 (1) { ["c"]=> float(129.2) } } ["d"]=> float(123.321) } object(stdClass)#1 (3) { ["a"]=> int(123) ["b"]=> array(3) { [0]=> object(stdClass)#2 (1) { ["c"]=> float(127.2) } [1]=> object(stdClass)#3 (1) { ["c"]=> int(128) } [2]=> object(stdClass)#4 (1) { ["c"]=> float(129.2) } } ["d"]=> float(123.321) } object(stdClass)#1 (3) { ["a"]=> int(123) ["b"]=> array(3) { [0]=> object(stdClass)#4 (1) { ["c"]=> float(127.2) } [1]=> object(stdClass)#3 (1) { ["c"]=> int(128) } [2]=> object(stdClass)#2 (1) { ["c"]=> float(129.2) } } ["d"]=> float(123.321) } Edited December 27, 2016 by NotionCommotion Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted December 27, 2016 Share Posted December 27, 2016 And that is important for what reason? PHP converts numeric types on the fly whenever necessary, so insisting on a float 128.0 as opposed to an integer 128 makes no sense. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted December 27, 2016 Author Share Posted December 27, 2016 And that is important for what reason? PHP converts numeric types on the fly whenever necessary, so insisting on a float 128.0 as opposed to an integer 128 makes no sense. The receiving machine needs float values. I am in control of both the sending machine and receiving machine, and I wish to put as much of the processing on the sending machine. Since I am in control of both machines, I didn't plan on validating on the receiving machine, but in hindsight, not doing so is likely never a good idea. Please ignore. I will send JSON in whatever format PHP wishes to do, and still validate/typecast on the receiving machine. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted December 27, 2016 Share Posted December 27, 2016 The receiving machine needs float values. Again: Why? As I already said, PHP and virtually all other mainstream languages can convert integer types on the fly: var_dump(128 + 0.5); // float(128.5) So what kind of application “needs floats” and is unable to handle 128 as opposed to 128.0? Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted December 27, 2016 Author Share Posted December 27, 2016 So what kind of application “needs floats” and is unable to handle 128 as opposed to 128.0? influxdb Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted December 27, 2016 Share Posted December 27, 2016 InfluxDB interprets all number literals as floats, unless you're using some very old version. Quote Link to comment Share on other sites More sharing options...
requinix Posted December 27, 2016 Share Posted December 27, 2016 As said, PHP does not distinguish between 123 and 123.0. If you must have 123.0 then you need to do that manually. Which would suck. Are you absolutely sure it has to be a float? Have you tried an integer and the service is returning an error? Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted December 27, 2016 Share Posted December 27, 2016 Early versions of InfluxDB did have issues with numeric types. However, this was fixed back in 2015, so I guess the answer is: Update your software. Quote Link to comment Share on other sites More sharing options...
Psycho Posted December 27, 2016 Share Posted December 27, 2016 (edited) One reason why you question makes no sense is that "128" can be a valid float value. var_dump(floatval(128)); //Output: float(128) If you must have the value "formatted" in a way that "looks" like a float (which seems doubtful) you can force the format using something like number_format(); $number = intval(128); echo number_format ($number, 1); //Output: 128.0 EDIT: Since you need to do that with array values you could apply that using array_walk() to all the values as needed. Edited December 28, 2016 by Psycho Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted December 28, 2016 Author Share Posted December 28, 2016 Thanks Jacques1, requinix, and Psycho, Yes, the version I am using will throw an error stating something like previous values were floats and the new value attempted to be inputted is an integer. The problem manifests when a value just happens to be 128.0 which PHP replaces with 128. I don't know whether influx just wants it to "look" like a float or really be one (and I am not sure this even makes any sense). I haven't been able to check the influx version I am using, but expect it is old. If it is, I will update it and hopefully this will be a mute issue. If it still is an issue (which I expect will not be), I will just iterate over the values on the receiving side and typecast as necessary. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted December 28, 2016 Author Share Posted December 28, 2016 I had been using influxdb 0.9.4.2. I am also using https://github.com/influxdata/influxdb-php as a PHP influxdb client. I never tried making an insert directly into the influx shell, so I don't know whether I would get the float/integer error. I've since upgraded to influxdb 1.1.1. I still get the float/integer error when using the influxdb-php client. I do not get the error when making an insert directly into the influx shell. Looks like I need a new influxdb client... Quote Link to comment Share on other sites More sharing options...
Solution Jacques1 Posted December 28, 2016 Solution Share Posted December 28, 2016 The client essentially subverts the type flexibility of InfluxDB by appending an explicit “i” whenever it encounters a PHP integer. If there's no better client, cast the arguments of the Point constructor: $point = new Point($measurement, (float) $value); Or change the source code. 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.