【问题标题】:How to encode attributeLables and value in JSON in yii 1?如何在 yii 1 中对 JSON 中的属性标签和值进行编码?
【发布时间】:2016-11-03 10:48:21
【问题描述】:

我有以下代码:

        $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 as First Name(first name is attributeLabels, or owner_name as Owner)):

{"First Name":"sdf","Owner":"sdfgsdfg","Telegram Number":null, "Address":null}

我该怎么做?

【问题讨论】:

    标签: php json yii yii1.x


    【解决方案1】:

    只需构造您自己的属性数组:

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

    【讨论】:

    • 只返回数组
    • @phpdev 谁?您是否将 CJSON::encode 分配给某事?
    猜你喜欢
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 2019-05-06
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    相关资源
    最近更新 更多