In PHP you can dynamically create variables
$myarr = array('name'=>'Adam', 'age'=>22, 'sex'=>'male');
foreach ($myarr as $k=>$v)
$$k = $v;
is there a way to pass a callback functionX and arrayX to another functionY, dynamically create variables from arrayX in functionY, and be able to reference those variables within functionX callback?
for example, I would like to:
function eachRecord($arr, $callback){
foreach ($arr as $k=>$v) $$k = $v;
$callback();
}
$myarr = array('name'=>'Adam', 'age'=>22, 'sex'=>'male');
eachRecord($myarr, function(){
echo "{$name} is a {$sex} of age {$age}.";
});
i don't want to have to pass variables back into the callback function, since i may not know the length or keys within the array, and i don't want to pollute the global scope with unknown variable names because they are created dynamically.
is there any way to do this? closures?
thanks