Yii2:如何转换 JSON PHP 数组的数据类型值? [英] Yii2: How to convert data type value of a JSON PHP array?
本文介绍了Yii2:如何转换 JSON PHP 数组的数据类型值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 Yii2 框架来创建这个 JSON 数组:
I am using Yii2 framework to create this JSON array:
"data": [
{
"id": 201,
"name": "John",
"age": "30"
}
]
年龄是一个字符串,我需要它是一个整数,即没有引号.像这样:
The age is a string and I need it to be an integer, that is, without quotation marks. Like this:
"data": [
{
"id": 201,
"name": "John",
"age": 30
}
]
这是创建 JSON 数组的 PHP 函数:
This is the PHP function that create the JSON array:
$persons['data'] = Persons::find()
->select([
'id',
'name',
'age'
])
->asArray()
->all();
推荐答案
您可以使用 JSON_NUMERIC_CHECK
标志作为 json_encode()
或 \yii\ 的第二个参数helpers\Json::encode()
用于自 php 5.3 起自动转换数字,请参见 此处
.
You can use JSON_NUMERIC_CHECK
flag as second parameter of json_encode()
or \yii\helpers\Json::encode()
for converting numbers automatically since php 5.3, see here
.
你可以使用像
$data = Persons::find()
->select(
[
'id',
'name',
'age'
]
)
->asArray()
->all();
$json = \yii\helpers\Json::encode($data,JSON_NUMERIC_CHECK);
这篇关于Yii2:如何转换 JSON PHP 数组的数据类型值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文