Variable Variables Pointing to Arrays or Nested Objects

后端 未结 5 1570
南方客
南方客 2020-12-04 03:01

Is it possible to create a variable variable pointing to an array or to nested objects? The php docs specifically say you cannot point to SuperGlobals but its unclear (to me

5条回答
  •  一生所求
    2020-12-04 03:25

    there is a dynamic approach for to many nested levels:

    $attrs = ['level1', 'levelt', 'level3',...];
    $finalAttr = $myObject;
    foreach ($attrs as $attr) {
        $finalAttr = $finalAttr->$attr;
    }
    return $finalAttr;
    

提交回复
热议问题