如何在yii 1中对attributeLables和JSON中的值进行编码?

时间:2016-11-03 10:48:21

标签: php json yii yii1.x

我有以下代码:

        $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}

我该怎么做?

1 个答案:

答案 0 :(得分:1)

只需构建自己的属性数组:

$model = new JsonForms();
$json = [];

foreach ($model->attributes as $attribute => $value) {
    if ($attribute != 'json') {
        $json[$model->getAttributeLabel($attribute)] = $value;
    }
}

CJSON::encode($json);