V
V
VitaliyKaliuzhyn2017-03-23 11:36:33
Yii
VitaliyKaliuzhyn, 2017-03-23 11:36:33

Output a specific part of an array?

there is an object that I get like this: it outputs
$model->getAttribute('help');

string '{
    "user": {
        "type": "class",
        "methodTitle": "modelTitle",
        "class": "\\gm\\models\\users\\tables\\Users",
        "fields": {
            "username" : "attribute",
            "first_name": "attribute",
            "last_name": "attribute",
            "second_name": "attribute"
        }
    }
}'

How can I display "username"

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry, 2017-03-23
@slo_nik

Good morning.
I understand that you have a connection in the getFields() model.
To get data through a connection, write like this:
More details here (working with connected data)
And supplement your question with the content of getAttribute('help'), what's inside, how does it work?

A
Andrey Mikhailov, 2017-03-23
@Andr-ik

Symfony has a PropertyAccess Component

use Symfony\Component\PropertyAccess\PropertyAccess;
$accessor = PropertyAccess::createPropertyAccessor();
echo $accessor->getValue(json_decode($model->getAttribute('help')), 'user.fields.username'));

This component can be used independently in any php project
composer require "symfony/property-access"

M
Maxim Timofeev, 2017-03-24
@webinar

$str = $model->getAttribute('help');
$array = json_decode($str);
echo $array['user']['fields']['username'];

you can read more about php here:
php.net/json_decode
In yii2, by the way, there is a helper for this: www.yiiframework.com/doc-2.0/yii-helpers-json.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question