I am trying to pass an index of associative array stored in a variable:
$a = array(
'one' => array('one' => 11, 'two' =>12),
'two' => array('one' => 21, 'two' => 22)
);
$s = "['one']['two']"; // here I am trying to assign keys to a variable for a future use
echo $a[$s]; // this usage gives error Message: Undefined index: ['one']['two']
echo $a['one']['two']; // this returns correct result (12)
The same I am trying to do within the object:
$a = array(
'one' => (object)array('one' => 11, 'two' =>12),
'two' => (object)array('one' => 21, 'two' => 22)
);
$a = (object)$a; // imagine that I receive this data from a JSON (which has much sophisticated nesting)
$s = 'one->two'; // I want to access certain object property which I receive from somewhere, hence stored in a variable
echo $a->$s; // produces error. Severity: Notice Message: Undefined property: stdClass::$one->two
echo $a->one->two; // gives correct result ("12")
How do I properly use a variable to access a value as mentioned in above examples? Note: wrapping these into Curly brackets (i.e. complex syntax) did not work in both cases.