Our PHP API outputs results using json_encode with JSON_NUMERIC_CHECK enabled, which is important for financial figures, binary values etc.. but we have recently introduced a phone number field, and it is removing zeros from the beginning of the number when it casts it as an integer.
I've tried to force "(string)" type on the value before it goes to json_encode, but it looks like the function is overriding it. eg:
$output = array(
'is_bool' => 1,
'total_amount' => '431.65',
'phone_number' => (string) '0272561313'
);
echo json_encode($output,JSON_NUMERIC_CHECK);
Produces:
{"is_bool":1,"total_amount":431.65,"phone_number":272561313}
Any suggestions on how to best handle this would be much appreciated. I really don't want to have to resort to adding trailing spaces at the end of the phone number when it's output just to get it through as a string.
Thanks!
You can try this:
{
"is_bool": 1,
"total_amount": 431.65,
"phone_number": "xn#0272561313"
}
as example in mysql: SELECT CONCAT('xn#', phone_number) as phone_number...
If you used json_encode()
, replace the string like this:
echo str_replace('xn#','',$your_encoded_json);
pass the data to this function to get JSON encoding with + and 0 preceded data staying as a string. I am using double encoding and str_replace to get over the issue.
public static function json_encode($data){
$numeric = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
$nonnumeric = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
preg_match_all("/\"[0\+]+(\d+)\"/",$nonnumeric, $vars);
foreach($vars[0] as $k => $v){
$numeric = preg_replace("/\:\s*{$vars[1][$k]},/",": {$v},",$numeric);
}
return $numeric;
}
Instead of typecasting it to a string do something like this:
$output = array(
'is_bool' => 1,
'total_amount' => '431.65',
'phone_number' => '"0272561313"' // add quotations
);
echo '<pre>';
echo json_encode($output,JSON_NUMERIC_CHECK | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
It'l keep the trailing zero:
{
"is_bool": 1,
"total_amount": 431.65,
"phone_number": "\"0272561313\""
}
Decode:
$test = json_encode($output,JSON_NUMERIC_CHECK | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
$test = json_decode($test, true);
print_r($test);
Output:
Array
(
[is_bool] => 1
[total_amount] => 431.65
[phone_number] => "0272561313"
)
Had the same issue with phone numbers, this walks over an array (no objects!) to cast only numeric values not starting with 0 or + to an int or float.
array_walk_recursive($json, function (&$value, $key)
{
if(is_string($value) && is_numeric($value))
{
// check if value doesn't starts with 0 or +
if(!preg_match('/^(\+|0)/', $value))
{
// cast $value to int or float
$value += 0;
}
}
});
return json_encode($json);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With