PHP returning Numeric Values in JSON

Source: Internet
Author: User

When I wrote about launching a prototype of a new joind.in APIs, quite a few people started to try it out. My friend David Soria Parra emailed me to point out this many of the numbers in the API were being returned as strings. He said:


It ' s just a standard problem of PHP REST services. When I try to access it with Java I has to convert it over and over again to INTs.


I did had a quick look at the PHP manual page for Json_encode but I didn ' t see anything mentioning this. A few weeks later (my inbox is a black hole and it takes a while to the process these things) I fell over a throwaway comment To a undocumented constant json_numeric_check, and I added the constant name to my todo list. In the time it took for me to actually get around to Googling for this, some wonderful person updated the PHP manual page (This is what I love PHP) to include it as a documented option, and someone else had added a user contributed note about Us ing it.


It turns out, this constant does exactly what I need. Here's a simple use case:

echo json_encode (Array (' event_id ' = ' 603 ')), Echo json_encode (Array (' event_id ' = ' 603 '), Json_numeric_check);

and the output:

{"event_id": "603"} {"event_id": 603}


There is probably some situations in which you don ' t want all your looks-like-a-number data to be returned as a number, b UT for now it seems to is a good fit for api.joind.in.


PHP returning Numeric Values in JSON

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.