1

I am trying to return information from a mysql database using php and need to get rid of the "" in the dynamic values. I have tried using real_escape_string and trim but this has not worked.

while($row = mysqli_fetch_assoc($result)) {

    $myArray= array(

   // array("running"=> 23),
   // array("walking"=> 54)

  
    array("running"=>$row['running']),
    array("walking"=>$row['walking'])

  );  
}

echo json_encode($myArray);

As the code currently stands it returns [{"running":"23"},{"walking":"54"}]

However, I need it to return [{"running":23},{"walking":54}] without the "" characters (It returns this using my commented out static code)

KeenLearnerA
  • 159
  • 3
  • 14
  • 2
    cast it as `(int) $row['whatever_number']` – Kevin Jul 28 '20 at 02:10
  • Seems like you're storing these numbers as strings in the database. I think if you used an actual number column, `mysqli` would already return these values in the desired type. I might be wrong though. – Phiter Jul 28 '20 at 02:11
  • Does this answer your question? [MySQL integer field is returned as string in PHP](https://stackoverflow.com/questions/5323146/mysql-integer-field-is-returned-as-string-in-php) – user3783243 Jul 28 '20 at 02:19

1 Answers1

1

You can use the JSON_NUMERIC_CHECK parameter in the json_encode().

echo json_encode($myArray, JSON_NUMERIC_CHECK);

JSON_NUMERIC_CHECK (integer)

Encodes numeric strings as numbers. Available as of PHP 5.3.3.

Max
  • 21
  • 2