我有以下代码:
$model = new JsonForms();
$model->name = $this->name;
$model->json = json_encode($this-> attributes);
$model->json = json_encode($this-> attributes);
按以下格式对数据进行编码:
{"t_name":"sdf","owner_name":"sdfgsdfg","telegram_number":null, "j_address":null}
这里t_name,owner_name等是变量。我需要保存标签名称而不是变量(例如t_name
作为名字(名字是attributeLabels,或owner_name
作为所有者)),格式如下所示:
{"First Name":"sdf","Owner":"sdfgsdfg","Telegram Number":null, "Address":null}
我该怎么做?
答案 0 :(得分:1)
只需构建自己的属性数组:
$model = new JsonForms();
$json = [];
foreach ($model->attributes as $attribute => $value) {
if ($attribute != 'json') {
$json[$model->getAttributeLabel($attribute)] = $value;
}
}
CJSON::encode($json);