-1

I can do:

$key = 'address';
$user->{$key}

Is there some way to make this work for deeper levels? I do not know in advance how many levels.

$key = 'address->street';
$user->{$key}

Here is what I am actually trying to achieve:

$filterIds = [];
$filterIds['facebook->followers'] = 1;
$filterIds['googleplus->followers'] = 2;
$filterIds['instagram->followers'] = 3;
...

    foreach($filterIds as $filterKey => $filterId) {
        if(self::property_path_exists($data, $filterKey)) {
            $filter = new ResultFilter;
            $filter->result_filter_id = $filterId;
            $filter->value = $data->{$filterKey};
            $filter->save();
        }
    }
Chris
  • 13,100
  • 23
  • 79
  • 162

1 Answers1

1

Example

<?php

$v = new stdClass;
$v->x = new stdClass;
$v->x->y = new stdClass;
$v->x->y->z = "wow";

$keys = "x->y->z";

$t = &$v;
foreach (explode("->", $keys) as $key) {
    // @TODO add isset($t->{$key})
    $t = &$t->{$key};
}

print($t . PHP_EOL);

$t = "changed";

print($v->x->y->z . PHP_EOL);

Remember to use reference (&) when you want to edit this nested object.

ventaquil
  • 2,780
  • 3
  • 23
  • 48