I need to do this:
$array['level1']['level2']['level3'] = 'someval';
However, I don't know how many levels there are going to be. I want to be able to create these arrays with any number of levels automatically. I am trying to adapt the following so that it forms an actual PHP array:
for($i=1;$i<=3;$i++){
$string .= '_level'.$i;
};
${'array'.$string} = 'someval';
var_dump($array_level1_level2_level3); //Outputs: string(7) "someval"
Obviously that is no replacement for an array. I'm simply looking for a parallel that can be applied to multidimensional arrays. I suspect the answer lies in some kind of recursive function, but I'm not quite sure what.
Here is what I really want to do. I've had trouble sufficiently explaining my problems and as a result the question was closed. So, I tried to take it apart into pieces.
https://stackoverflow.com/questions/8002490/creating-multidimensional-arrays-in-php
You may try the following:
function makeArray($array, $lowElement, $highElement, $value) {
if($highElement == $lowElement) {
$array['level' . $highElement] = $value;
}
else {
$array['level' . $lowElement] = makeArray($array, $lowElement+1, $highElement, $value);
}
return $array;
}
$array = array();
$array = makeArray($array, 1, 5, 'someval');
print_r($array); // Output: Array ( [level1] => Array ( [level2] => Array ( [level3] => Array ( [level4] => Array ( [level5] => someval ) ) ) ) )
Output indicates that now you have the following:
$array['level1']['level2']['level3']['level4']['level5'] = 'someval';