PHP json_encode scientific format - php

I am trying to encode the number 17342846 but every time i get 1.7342846e+07 instead of the int format. i am using
$var = 17342846;
json_encode((int)$var);
in php trying to get it to work but still no luck. Any help would be appreciated.

just encode it as string format then convert it later.
json_encode($var + '');

Related

Convert non-alphanumeric characters to unicode codes

I receive a token in this format :
{"signature": "MEQCIFf4uQXQYR6fA48cHZMwR5K0bO/wsK5ygoCukmAfWslIAiAdc4kN1BEixxrreSI3W3x4a92+fFTw7/Ulqw9RuJPRzg\u003d\u003d","protocolVersion": "ECv1", "signedMessage": "{\"encryptedMessage\":\"HKdqg8pDCiAwaHmGeI+/7xIDXXCTSfK+/SERLh94NBX6l99w7vNgBenMCiaAGvO+nbHkmnaOnwMcq/DpRhFtCJuYjAGKA83UePYjleSgXp8AjTKUweXxpqNEVvexSeflHBQNcx4stvB7lhsCeW9SMhecebfkcgQyGlawBECXrsIWhfIRGHklC6KE18tlA0GfvsQLhKreWspHCxQjgiBDim6uR57aKzTzlTFGYK+IB1mMJbVFTrEeBnKOAlvdt8Nh4BH3DhrmV3HVl+Ydc9V2G6iGZ6EmPxe3QG5dC9aYGollEXieasTFZm1Bt/LQMdyHmQEd+cmdIQNfGhxzz5pWpLP9g8LuoG+8h69TYaVFY2o0FjP2vSuPqGhMlXhcWgb/gJsiAOLGkS2ZdFbhpQg3kEyS5f/h91Wuoxy08JHpFxsvzWL3skfJ5eQc/BykvHyzzxzK\",\"ephemeralPublicKey\":\"BFnxpfpfIeFLmJ/KM/GcQyhU0MBlEReejnKa71gKFnV+5N2t3WNBnaaNu02gjy7Z5d07XO5+O77Qx3abHnw5rwk\\u003d\",\"tag\":\"kbnLkGZKyDKBWQAh6AyCNL55V4SF8DiZ4PeIudtKBH0\\u003d\"}"}
I need to json_decode it, do something with it then push it back. If I json_decode it, I will get the \u003d character decoded to =, which is fine. But how do I encode it back ? I think I played with every possible conversion function in PHP and I'm fresh out of ideas.
Here's a sandbox to play around with : http://sandbox.onlinephpfunctions.com/code/b93defec84a9a6f22f5085fbb7628b5afa816d95
In normal work you do not need to encode = to \u003d but if you must use somthing like this:
$encoded = str_replace('=','\\u003d', $encoded);
Note about your sandbox example:
In your example the token variable signedMessage is json in json
but after changes return as single json and not json in json like before changes.
maybe its fine for your application but check it

Convert MySQL's POINT to text in PHP

Using PHP, how can I convert a value in POINT datatype to a string like POINT (-34.601020 -58.371020) (an ouput in WKT or GeoJSON is preferable)
If I echo the raw value, I get weird characters.
I've tried using bin2hex and then tried to convert the hex to string but with no luck.
I'm aware of MySQL's AsText(), but I would like to do it in PHP.
Finally I've got this working!!!
I had to use unpack in order to extract the binary data from MySQL
$point_value = $data_from_db["point_field"];
$coordinates = unpack('x/x/x/x/corder/Ltype/dlat/dlon', $point_value);
echo $coordinates['lat'];
echo $coordinates['lon'];
Here is a tutotial that helped me with this issue

Accessing POST data

I'm very new to PHP but have a good understanding of C,
When I want to access some post data on an API i'm creating in PHP I use:
$_POST['date_set']
to fetch a value being passed for date - This all works perfectly, however I read I should be fetching it like this:
$date_set = trim(urldecode($_POST['date_set']));
This always returns a 00:00:00 value for the date after it's stored in my DB.
When I access directly using $_POST['date_set'] I get whatever value was posted, for example: 2013-08-28 10:31:03
Can someone tell me what I'm messing up?
You should try it like,
$date_set = $_POST['date_set'].explode(' ');//('2013-08-28 10:31:03').explode(' ')
echo $date_set[1];
or
echo date('H:i:s',strtotime($_POST['date_set'])));
//echo date('H:i:s',strtotime('2013-08-28 10:31:03'));
If you are very new in php the Read date()
You only run urldecode over data is URL encoded. PHP will have decoded it before populating $_POST, so you certainly shouldn't be using that. (You might have to if you are dealing with double-encoded data, but the right solution there should be to not double encode the data).
trim removes leading and trailing white-space. It is useful if you have a free form input in which rogue spaces might be typed. You will need to do further sanity checking afterwards.
urldecode — Decodes URL-encoded string
Description
string urldecode ( string $str )
Decodes any %## encoding in the given string. Plus symbols ('+') are decoded to a space character.
urldecode: is used only for GET requests. you should be fine using $_POST['date_set'] only.
http://php.net/manual/en/function.urldecode.php
You'd better do this way
if(isset($_POST['date_set'])){
$date_set = $_POST['date_set'];
}
then you can use $date_set how you want.
If you still get 00:00:00 for $date_set, the problem is coming from the code which provide you the $_POST value.

How to perform an execute command using a JSON variable?

I've got a JSON variable ($epoch) which contains the date in epoch (1370777177), and I'd like to convert it to the following format and output it:
date "+%F %R" -ud #1370777177
which returns:
2013-06-09 11:26
So I figure using php to echo the command is one way to do it. Is there a way to convert the epoch to the format above and append it to a DIV using JS? That might be a lot easier. I've tried creating a PHP variable which encodes the JSON var:
$date = json_encode($epoch);
echo exec ('date "+%F %R" -ud #%date');
But that doesn't work.
echo json_encode($epoch)
returns 'null'. The JSON variable is in a external .js file.
Is there a way to convert the epoch to the format above and append it to a DIV using JS?
Yes, there is.
Also exec() will scale awesomely.

How to convert URL encoded JSON array into PHP array

how can convert URL encoded JSON sent from my android application into PHP array.Current format i am getting looks like below format.
[{\\\"product_id\\\":\\\"33\\\",\\\"amount\\\":\\\"1\\\"},{\\\"product_id\\\":\\\"34\\\",\\\"amount\\\":\\\"3\\\"},{\\\"product_id\\\":\\\"10\\\",\\\"amount\\\":\\\"1\\\"}]
How can i convert those data into below format
product_id amount
33 1
34 3
10 1
Because i want to insert those data into MySQL database.Can anyone please help me regarding this problem.
Use json_decode :
echo json_decode($jsonarr);
this can be done by json_decode
$json = '{"foo": 12345}';
$obj = json_decode($json);
print $obj->{'foo'}; // 12345
Good read
--- common mistakes using json_decode()
json_decode(jsonencoded string, TRUE) is correct way to encode json string.
But your json string encoded too deeper and recursive. your json encode string should as below
[{"product_id":9,"amount":500},{"product_id":9,"amount":500},{"product_id":9,"amount":500}]
Other wise, you will get NULL value

Categories