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?

4
  • What do you want the function to do? Commented Mar 2, 2014 at 14:07
  • Yes, via $tables[$i]['form'](); Commented Mar 2, 2014 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. Commented Mar 3, 2014 at 7:55
  • possible duplicate of Create functions in a loop with names from elements in an array in php Commented Mar 3, 2014 at 15:46

1 Answer 1

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'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.