2

I have the following variable with some coordinates from google maps:

$coordinates = '(22.2819939, 114.15444100000002)';

So to separate them I did the following:

$coor = explode(',',str_replace(array('(',')'),'',$coordinates));

Now I need to send this coordinates to an API in the following format:

$message = array("location"=>array($coor[1],$coor[0]));

I must send this in json so I encode the array but I am getting the coordinates as strings and not as number:

$toSend = json_encode($message);
result-> {"location":["114.15444100000002","22.2819939"]}

How can I avoid json to take the coordinates as string and take them as number instead?

I need this result:

{"location":[114.15444100000002,22.2819939]}
6
  • How about $message = array("location"=>array((float)$coor[1], (float)$coor[0])); ? Commented Jun 7, 2016 at 2:52
  • hey mate, it modify the number... in $coor[1] I lost 0000002 Commented Jun 7, 2016 at 3:08
  • waht happened if you use double (double) "114.15444100000002" Commented Jun 7, 2016 at 3:11
  • 2
    114.15444100000002 looks incorrect. Google Maps provides 6 decimal points of precision, afaik. I think you hit a floating point precision issue before it got stored. 114.154441 is the number you want, pretty sure. Commented Jun 7, 2016 at 3:16
  • 2
    See this post on GPS precision. 6 decimals gets you 0.11m of precision. There's no way you really want 14 decimals anyway. Commented Jun 7, 2016 at 3:17

4 Answers 4

3

You'll need to convert them from string to float. So we simply map the array with a float conversion

$coor = array_map('floatval', $coor);
Sign up to request clarification or add additional context in comments.

4 Comments

hey mate is doenst work because it modified the number... i get "location":[114.154441,22.2819939]... now I dont have the string but I lost 000002 from the original number.... other idea?
And what's up with the backticks instead of quotes?
with quotes the number changes, with backticks is still get an string
I wonder why this answer is accepted. Backticks instead of quote ?
3

See JSON Predefined Constants and use the JSON_NUMERIC_CHECK option (but you will lose some precision):

$toSend = json_encode($message, JSON_NUMERIC_CHECK);

JSON_NUMERIC_CHECK (integer)

Encodes numeric strings as numbers. Available since PHP 5.3.3.

3 Comments

OP is worrying that he will lose precision on those values.
The only way to keep the precision is to keep it a string. How will the receiving code keep the precision?
@Raptor: OP had string 114.15444100000002 and you used number_format() to produce string 114.15444100000002. Unnecessary...
1

You can cast the strings into numbers

$number = (float) "114.15444100000002";

2 Comments

please check my coment above, same problem with float before... it modify my number
It is because float has limited precision in PHP (See this). You got 14 sig. fig.!
0

You can make use of number_format() to achieve your goal. See this example below:

$num_str = "114.15444100000002";
$str2float = (float)$num_str;
echo 'Cast to Float: ' . $str2float . PHP_EOL;

$num_format = number_format($num_str, 14);
echo 'With number_format(): ' . $num_format . PHP_EOL;

The result will be:

Cast to Float: 114.154441
With number_format(): 114.15444100000002

Proven number_format() works in your case.


p.s. you should make use of this function after you parsed the JSON. You should leave your values in string to prevent loss of precision during encoding and decoding.

p.s. As @jszobody mentioned, if the values are really coordinates, the precision should never require to go beyond 6 decimal places.

1 Comment

Except, 14 decimal places in a GPS coordinate is absurd. Even seven decimals gets you to millimeters of precision.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.