将一些json字符串的值转换为php中的整数

dea*_*zvi 7 php arrays json

我有以下PHP代码:

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

$data_string = json_encode($data);
Run Code Online (Sandbox Code Playgroud)

示例JSON如下:

{
 "id":"7",
 "name":"Dean",
 "country":"US",
 "currency":"840",
 "description":"Test"      
}
Run Code Online (Sandbox Code Playgroud)

我需要将"id"字段设为整数,并将"currency"保留为字符串,以便JSON成为:

 {
 "id":7,
 "name":"Dean",
 "country":"US",
 "currency":"840",
 "description":"Test"      
}
Run Code Online (Sandbox Code Playgroud)

我试过用:

$data_string = json_encode($data, JSON_NUMERIC_CHECK);
Run Code Online (Sandbox Code Playgroud)

但它也将"货币"变为整数.

有什么方法可以使"id"成为整数并将货币作为字符串.

Mam*_*mta 6

Type casting得像

$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);
Run Code Online (Sandbox Code Playgroud)