Encode value to JSON as a number

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
DJ_AM_JuiceboxAsked:
Who is Participating?
 
marchentConnect With a Mentor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.