PHP json_encode encoding numbers as strings
I've done a very quick test :
$a = array(
'id' => 152,
'another' => 'test',
'ananother' => 456,
);
$json = json_encode($a);
echo $json;
This seems to be like what you describe, if I'm not mistaken ?
And I'm getting as output :
{"id":152,"another":"test","ananother":456}
So, in this case, the integers have not been converted to string.
Still, this might be dependant of the version of PHP we are using : there have been a couple of json_encode related bugs corrected, depending on the version of PHP...
This test has been made with PHP 5.2.6 ; I'm getting the same thing with PHP 5.2.9 and 5.3.0 ; I don't have another 5.2.x version to test with, though :-(
Which version of PHP are you using ? Or is your test-case more complex than the example you posted ?
Maybe one bug report on http://bugs.php.net/ could be related ? For instance, Bug #40503 : json_encode integer conversion is inconsistent with PHP ?
Maybe Bug #38680 could interest you too, btw ?
How can I force PHP's json_encode integer values to be encoded as String?
In the link posted by Thomas in comments, one user suggests that you do this:
$data = json_encode(array_map('strval', $data));
This might not be the most efficient in terms of performace though, since every entry on the array will pass through the strval
function.
PHP JSON encode output number as string
You could also try
echo json_encode( $results, JSON_NUMERIC_CHECK );
Some reference:
PHP JSON constants
PHP json_encode creates number index as string instead of an object
Because the array doesn't start with index 0
but with index 1
, it's encoded as an JSON object instead of an JSON array.
You can use the array_values()
function to remove the indexes and only keep the values.
Example:
$ret['testing'] = array_values($ret['testing'])
echo json_encode($ret);
But because you don't need the index at this moment, you can also refactor your code to this:
foreach($data as $index => $value){
if($index < 1){
$ret['test'][] = array(
'val' => $value,
'me' => 'index < 1'
);
}
else {
$ret['testing'][] = array(
'val' => $value,
'me' => 'index >= 1'
);
}
}
echo json_encode($ret);
This way, the arrays will always start with index 0
.
PHP - JSON_ENCODE Number converted to string. How to fix it?
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);
PHP json_encode outputs numbers with quotes
I think that you only need to convert the type of your values in PHP, like this:
$data[$j] = [$res->shortName, intval( $res->col1 ) ];
This way all your $res->col1
values will be stored as int. If you want float values just use floatval
instead of intval
, like this:
$data[$j] = [$res->shortName, floatval( $res->col1 ) ];
json_encode change number into string
I am guessing online you have an older version of PHP:
JSON_NUMERIC_CHECK (integer)
Encodes numeric strings as numbers. Available since PHP 5.3.3.
When you JSON encode, it will not have quotes if PHP knows it is not a string.
If you need to do it manually, you could do something like this:
<?php
function json_numeric($array)
{
if (is_array($array) || is_object($array)) {
foreach($array as &$prop) {
if (is_numeric($prop)) {
$prop = intval($prop);
}
if (is_object($prop) || is_array($prop)) {
$prop = json_numeric($prop);
}
}
}
return $array;
}
$x = array("a" => 1, "b" => "2", "c"=>array("d"=>1, "e"=>"2"));
echo json_encode(json_numeric($x));
//{"a":1,"b":2,"c":{"d":1,"e":2}}
$y = new stdClass();
$y->a = 1;
$y->b = "2";
echo json_encode(json_numeric($y));
//{"a":1,"b":2}
?>
Numbers in quotes using json_encode()
If you're building your json data from a one-dimensional array, you can use
echo json_encode(array_map('strval', $data));
This technique will unconditionally convert all values to strings (including objects, nulls, booleans, etc.).
If your data might be multidimensional, then you'll need to call it recursively.
Related Topics
Send Post Data via Raw JSON with Postman
How to Load Return Array from a PHP File
What Rss Parser Should I Use in PHP
How to Create a Random Hash/String
Why Are $_Server["Php_Auth_User"] and $_Server["Php_Auth_Pw"] Not Set
Write Utf-8 Characters to File with Fputcsv in PHP
Warning: Cannot Modify Header Information - Headers Already Sent
Environment Driven Database Settings in Laravel
PHP Call_User_Func VS. Just Calling Function
PHP Preg_Replace/Preg_Match VS PHP Str_Replace
Send Email from Localhost with Gmail(Windows)
PHP Classes: When to Use :: VS. ->
PHP Preg_Match - Only Allow Alphanumeric Strings and - _ Characters
How to Retrieve Youtube Video Details from Video Url Using PHP