I have a php code to convert an array to json:
$output = array(
'name' => "annie",
'is_bool' => 1,
'total_amount' => '431.65',
'phone_number' => '0272561313'
);
echo json_encode($output,JSON_NUMERIC_CHECK);
but JSON_NUMERIC_CHECK
removed 0 numbers at first value.
I want this output:
{"name":"annie","is_bool":1,"total_amount":431.65,"phone_number":0272561313}
my $output
initial of a query of database.
2
Answers
I’m afraid there’s no way to do what you’re trying to do with a PHP number.
The leading zero on the phone number won’t be respected if it’s cast to a float or integer. Neither will country codes, such as +44.
Cast it as a string; it’s all you can do.
So:
This part makes no sense:
There is no point in adding leading zeros to numbers, any decent library used to parse the resulting JSON will automatically remove them, because that is how numbers work. Phone numbers are called numbers partly for historial reasons (from the era before telephone exchanges were fully automated), partly because it’s informal English.
I suspect what you really want is to de-stringify some known numeric fields: