0

I have a loop that iterates through values in an array.

for ($i = 0; $i < count($tables); $i++) {
    $tables[$i]['form'] = function() {
    // function stuff
    };
}

How can I now call my functions using the values from the array as the function names?

Mark
  • 195
  • 1
  • 18
  • What do you want the function to do? – Amal Murali Mar 02 '14 at 14:07
  • Yes, via `$tables[$i]['form']();` – Bram Mar 02 '14 at 14:09
  • The function generates HTML for a plugin options page that sits in the Wordpress admin area. There are a number of tabs that allow users to view, edit and delete records in database tables. Each function represents the view/edit/delete options for a different table. Since users can add or delete their own tables, there needs to be a way of dynamically generating these pages. – Mark Mar 03 '14 at 07:55
  • possible duplicate of [Create functions in a loop with names from elements in an array in php](http://stackoverflow.com/questions/22127351/create-functions-in-a-loop-with-names-from-elements-in-an-array-in-php) – Jeff Lambert Mar 03 '14 at 15:46

1 Answers1

1

You can call it directly:

    $tables[1]['form']();

Or with call_user_func:

   call_user_func($tables[$i]['form']);

Parameters to your function are passes as additional parameters to the call_user_func() for example:

 $tables[1]['form'] = function($a){echo $a;};
 call_user_func($tables[1]['form'], 'booo');
 //would echo out 'booo'
Ray
  • 40,256
  • 21
  • 101
  • 138