1
 $con = new DBConnector();
 $sth =  $con->Query("SELECT medicinename as label, factor as data FROM medicine_master_tbl limit 4");
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
         $rows[] = $r;
      }
echo json_encode($rows);

there is no problem with my query,but the returned value is ..

[{"label":"rrp","data":"5"},{"label":"t","data":"34"},{"label":"tt","data":"4"},{"label":"nutrachin","data":"45"}]

i need the json array as like below..

[{"label":"rrp","data":5},{"label":"t","data":34},{"label":"tt","data":4},{"label":"nutrachin","data":45}]

which the data is considered as string in this array , i need to be parse it as integer .. thanks in advance.

3
  • what is the output of var_dump($r); Commented Jan 8, 2015 at 9:25
  • According to the php docs it should be output how you want it, what do you get if you var_dump/print_r $r inside the while? Commented Jan 8, 2015 at 9:26
  • So convert it to integer then. Commented Jan 8, 2015 at 9:27

2 Answers 2

2

An easy one.

while ($r = mysql_fetch_assoc($sth)) {
     $r["data"] = intval($r["data"]);
     $rows[] = $r;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Ideally your database connector would allow you to specify what type of data you are returning in the row, if factor is a numeric type. For instance, PDO and mysqlnd can return native types (see How to get numeric types from MySQL using PDO?).

However, you can do the following:

while ($r = mysql_fetch_assoc($sth)) {
    $r['data'] = intval($r['data']);
    $rows[] = $r;
}

This way, your JSON encoding will have an integer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.