I have a specific issue. I need to get and set values within a nested array. The array differs from occasion to occasion (dynamic) but I have access to another array (let's call it parent array) that contains the path to the nested values.
The array (let's call it main array) with the value that I want to access or change can look like this:
$form = array(
'field => array(
'und' => array(
0 => array(
'some_key' => 'foo'
)
)
)
);
..and in that case the parent array looks like this:
$parents = array(
'field',
'und',
0,
);
But the main array can also look like this:
$form = array(
'field => array(
'und' => array(
0 => array(
'another_field' => array(
'und' => array(
0 => array(
'some_key' => 'foo'
)
)
)
)
)
)
);
The parent array then looks like this.
$parents = array(
'field',
'und',
0,
'another field',
'und'
0
);
I want to be able to access values that are in a nested array, i.e:
$value = $form['field']['und'][0]['some_key']
And I can't do that since the value I need will be in a different place the next time around. The only thing that informs me of how deep the value is in the nested array is the parent array.
If it was a fixed array I would be able to do like this:
$value = $form[$parent[0]][$parent[1]][$parent[3]]['some_key'];
But it's not..
How do I go about and construct the.. the.. meh, I don't even know what it's called. Path? I want to construct the above $value
array based on the information found in the parent array.
I'd like to be able to do something like:
$path = '';
foreach ($parents as $key => $value) {
$path = $path . '[' . $value . ']';
}
$value = $form . $path . ['some_key'];
Note: For anyone wondering this is Drupals' Form API at work, but the question is more about PHP than Drupal.