I want to pass multiple parameters to a function in an array. The parameters themselves are arrays of strings.
I have a function called new_action(), which I call in the following way:
new_action(array('actions' => array('ACTION1','ACTION2'), 'tables' => array('table1','table2')));
and that function looks like this:
public function new_action($params=array()) {
$tables = array();
$actions = array();
if(count($params)) {
foreach($params as $k => $v) {
$tables = ($k == 'tables') ? $v : array();
$actions = ($k == 'actions') ? $v : array();
}
// Do useful stuff with the variables here ...
var_dump($tables);
var_dump($actions);
// ======== ACTUAL OUTPUT: ========
// array(0) { }
// array(0) { }
// ======== EXPECTED OUTPUT: ========
// array(2) { [0]=> string(6) "table1" [1]=> string(6) "table2" }
// array(2) { [0]=> string(7) "ACTION1" [1]=> string(7) "ACTION2" }
}
}
As you can see, the value of $v inside the loop (the array of strings) is never being copied to the variable $tables or $actions outside the loop.
I think this is most likely a scope problem but looking at answers to other questions similar to this hasn't elucidated the solution for me.
How do I refactor the code so that I can access those arrays stored in $v for each $k outside the foreach loop?