[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Encode value to JSON as a number

Posted on 2010-01-04
1
Medium Priority
?
298 Views
Last Modified: 2012-05-08
Hi,

I'm fetching rows of data from a mysql database, in order to return it as a JSON object later on. Looks like this:

  $rows = array();
  $result = mysql_query("SELECT * FROM mytable");
  while ($row = mysql_fetch_assoc($result)) {
      $rows[] = $row;
  }

  echo json_encode($rows);

one of my fields in the database is of type DOUBLE. But when I look out the output JSON object, that key/value pair is encoded as a string (has quotes around the value):

  "average": "1.33"

but it should be:

  "average": 1.33

what's the right way to do that?

Thanks
0
Comment
Question by:DJ_AM_Juicebox
1 Comment
 
LVL 13

Accepted Solution

by:
marchent earned 2000 total points
ID: 26174343
don't know any better way, but this can be tricky.

##  echo json_encode($rows);
echo preg_replace( '/(":\s?)"(\d+\.\d+)"/', "$1$2", json_encode($rows) );
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question