arrays - Converting some values of a json string to an integer in php -


i have following php code:

$data = array( 'id' => $_post['id'], 'name' => $_post['name'], 'country' => $_post['country'], 'currency' => $_post['currency'], 'description' => $_post['description'] );  $data_string = json_encode($data); 

the sample json follows:

{  "id":"7",  "name":"dean",  "country":"us",  "currency":"840",  "description":"test"       } 

i need make "id" field integer , keep "currency" string json becomes this:

 {  "id":7,  "name":"dean",  "country":"us",  "currency":"840",  "description":"test"       } 

i tried using:

$data_string = json_encode($data, json_numeric_check); 

but turns "currency" integer.

is there way can make "id" integer , leave currency string.

use type casting like

$data = array( 'id' => (int) $_post['id'],// type cast 'name' => $_post['name'], 'country' => $_post['country'], 'currency' => $_post['currency'], 'description' => $_post['description'] );  $data_string = json_encode($data); 

Comments