Essentially, I have a multidimensional array like this (it's actually much more extensive but this is the bare skeleton I need to access):
$arr = [
1 => [
'val1' => 'foo',
'val2' => 'bar',
],
2 => [
'type1' => [
'val3' => 'baz',
'val4' => 'fuz',
],
],
]
I will be accessing this array dynamically and won't know whether I'll be accessing $arr[1] or $arr[2] until I try to do so. Therefore, I also don't know how many levels deep I need to go until that time comes. I was looking for a function that accesses a varying number of levels deep of a multidimensional array but I can't find anything like that. I know I can do this with an if or switch statement but I'm trying to simplify it as something like:
if ( isset( $arr[$var]...... ) ) {
return $arr[$var]......;
}
My problem is that I can't figure out how to build a string to replace the "......" with the proper number of indices. I tried making a string using a loop:
function getAccessString( array $arrOfIndices ) {
$str = '';
foreach( $arrOfIndices as $index ) {
$str .= '['.$index.']';
}
return $str;
}
However, I couldn't get any variation of that string-making function to work in the sense that I could concatenate it in a way that would access my multidimensional array.
P.S. If there is no way to access this array in a very simple format along the line of this...
if ( isset( $arr[$var]...... ) ) {
return $arr[$var]......;
}
... I will simply use a switch statement. Using iterators, parsing the array and whatnot are not going to be simpler than the switch statement I've already got.