The problem is not a variable variable but the fact, that you are accessing a variable which is out of the function scope. From inside of function you can access only the parameters of the function, so called superglobals and global variables (with statement global $global_variable;
).
Pleae read about variable scope
If the $bookPrices is a global variable then you might try:
function keyValMyArray($nameofmyarray) {
global ${$nameofmyarray};
foreach(${$nameofmyarray} as $key => $val) {
print "$key: $val";
}
}
But this is not good practice, as it will only work in the special cases where you use global variables.
Maybe you can just pass keyValMyArray the actual variable, and not its name? It would be more general. That way if you can access the variable in the place where you call the funciton it would work also for local variables:
function keyValMyArray($myarray) {
foreach($myarray as $key => $val) {
print "$key: $val";
}
}
//then use:
keyValMyArray($bookPrices);
//or
$nameOfArray = 'bookPrices');
keyValMyArray(${$nameOfArray});
Best regards,
SWilk