phpdev
phpdev

Reputation: 511

How to encode attributeLables and value in JSON in yii 1?

I have following code:

        $model = new JsonForms();
        $model->name = $this->name;
        $model->json = json_encode($this-> attributes);

And $model->json = json_encode($this-> attributes);encodes data in the following format:

{"t_name":"sdf","owner_name":"sdfgsdfg","telegram_number":null, "j_address":null}

Here t_name, owner_name etc. are variables. I need to save label names instead of variables (e.g t_name as First Name(first name is attributeLabels, or owner_name as Owner) ) in the format which displays below:

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

How can I do it?

Upvotes: 1

Views: 295

Answers (1)

Justinas
Justinas

Reputation: 43479

Simply construct your own array of attributes:

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

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

CJSON::encode($json);

Upvotes: 1

Related Questions