I have an array $params with the following content:
Array(
0 => "array"
1 => "arrayName" //variable
2 => "key1" //optional&variable ie. 0 or 'foo'
3 => "key2" //optional&variable ie. 0 or 'foo'
//etc.
)
Also, I have an stdObject that contains all my variables for the page that is being requested (MVC style).
So, I could have an array in there like this:
$std = new stdObject();
$std->arrayName->array('foo', 'bar');
Now I want the first value of the $std array. So I am using the “key1” parameter in $params and set this to ‘0’ so it will pick the $std->arrayName[0] value (‘foo’). Note that I am not using the “key2” parameter since I don’t want to select $std->arrayName[key1][key2].
But what if I need the value of a nested array?
$std = new stdObject();
$std->arrayName->array('foo', array('bar', 'fish'));
I will request the $std->arrayName[1][1] value (‘fish’) by setting the “key1” parameter to 1 and the “key2” parameter to 1 as well. So it will select $std->arrayName[key1][key2].
But what if I have 10 arrays nested in each other? highly unlikely, but it’s possible. I want to select $std->arrayName[key1][key2][key3][..][key10] to select a certain value that’s located there.
For example: $std->arrayName[0][3][4][6][2][9][‘foo’][‘bar][9][2];
So my question is: how do I select the value of a certain array (arrayName) by the values defined in another array ($params) that act as the keys for the first array (arrayName). But, the amount of keys (values in $params) is optional.
Maybe the question isn’t completely understandable, so feel free to ask more information.
You need to loop the key array and assign each level to a variable, until you reach the end when you should have the value you want.
Something like this (untested, no error checking):