I'm working on a function that looks like this:
public function myfunction($mainArr, $keys) {
// $mainArr: a nested associative array
// $keys: a simple array of strings, example: array('string_1', 'string_2', 'string_3')
$totalKeys = count(keys);
if(totalKeys == 1) {
return mainArr[keys[0]];
} else if(totalKeys == 2) {
return mainArr[keys[0]][keys[1]];
} else if(totalKeys == 3) {
return mainArr[keys[0]][keys[1]][keys[2]];
} else if(totalKeys == 4) {
return mainArr[keys[0]][keys[1]][keys[2]][keys[3]];
} else if(totalKeys == 5) {
return mainArr[keys[0]][keys[1]][keys[2]][keys[3]][keys[4]];
}
// the same pattern continues..
}
I want to change this function into something more dynamic rather than a big list of "if" conditions, is it possible somehow?