title: How do I create a function inside of a foreach loop php?
body: <p>Theres this wordpress plugin called ninja forms, <a href="http://developer.ninjaforms.com/codex/merge-tags/" rel="nofollow noreferrer">http://developer.ninjaforms.com/codex/merge-tags/</a></p>
  
  <pre><code>        /* Individual tag registration. */
          $this-&gt;merge_tags = array(
  
              'foo' =&gt; array(
                'id' =&gt; 'foo',
                'tag' =&gt; '{my:foo}', // The tag to be  used.
                'label' =&gt; __( 'Foo', 'my_plugin' ), // Translatable label for tag selection.
                'callback' =&gt; 'foo' // Class method for processing the tag. See below.
            ),
          );
  
          /*
           * Use the `init` and `admin_init` hooks for any necessary data setup that relies on WordPress.
           * See: https://codex.wordpress.org/Plugin_API/Action_Reference
           */
          add_action( 'init', array( $this, 'init' ) );
          add_action( 'admin_init', array( $this, 'admin_init' ) );
        }
  
        public function init(){ /* This section intentionally left blank. */ }
        public function admin_init(){ /* This section intentionally left blank. */ }
  
        /**
         * The callback method for the {my:foo} merge tag.
         * @return string
         */
        public function foo()
        {
          // Do stuff here.
          return 'bar';
  </code></pre>
  
  <p>}
  }</p>
  
  <p>Value of 'callback' is then used as a function, public function(foo). </p>
  
  <p>I have added this to the array:  </p>
  
  <pre><code>[foo] =&gt; Array
      (
          [id] =&gt; foo
          [tag] =&gt; {my:foo}
          [label] =&gt; Foo
          [callback] =&gt; foo
      )
  
  [surveyid] =&gt; Array
      (
          [id] =&gt; surveyid
          [tag] =&gt; {my:surveyid}
          [label] =&gt; Surveyid
          [callback] =&gt; surveyid
      )
  
  [membername] =&gt; Array
      (
          [id] =&gt; membername
          [tag] =&gt; {my:membername}
          [label] =&gt; Membername
          [callback] =&gt; membername
      )
  </code></pre>
  
  <p>Ive added more arrays with the same format to this array, and id like to make their 'callback' values to public functions as they have.</p>
  
  <pre><code> /**
     * The callback method for the {my:foo} merge tag.
     * @return string
     */
    public function foo()
    {
      // Do stuff here.
      return 'bar';
    }
  </code></pre>
  
  <p>Though I plan to do this many times over and I may add more arrays in the future. So I am trying to dynamically assign the public function for each arrays callback value. </p>
  
  <p>This is what I have.</p>
  
  <pre><code>        $data = array(
  
          '@attributes' =&gt; array(
              'surveyid' =&gt; 'V54236',
              'membername' =&gt; 'John Smith',
  
              ));
  
      $realThing = array();
      foreach($data['@attributes'] as $key =&gt; $value) {    
          $realThing[$key] = array(
              'id' =&gt;  $key,
              'tag' =&gt; '{my:'.$key.'}', 
              'label' =&gt; __( ucfirst($key), 'my_plugin' ),
              'callback' =&gt; $key
  
     );
  }
  
      $this-&gt;merge_tags = $realThing;
  
          add_action( 'init', array( $this, 'init' ) );
          add_action( 'admin_init', array( $this, 'admin_init' ) );
        }
  
        public function init(){ /* This section intentionally left blank. */ }
        public function admin_init(){ /* This section intentionally left blank. */ }
  
      }
  </code></pre>
  
  <p>My attempt to assign functions for each callback value. </p>
  
  <pre><code>        foreach($realThing as $key =&gt; $value){
               public function $key['callback'](){
                   return $data['@attributes'][$key];
               }
           };
  </code></pre>
  
  <p>desired output: </p>
  
  <pre><code>  public function foo()
    {
      // Do stuff here.
      return 'bar';
    }
  
    public function surveyid()
    {
      // Do stuff here.
      return 'V54236';
  
    public function membername()
    {
      // Do stuff here.
      return 'John Smith';
  </code></pre>
  
  <p>All help appreciated.</p>
  
  <p>also getting:  syntax error, unexpected 'foreach' (T_FOREACH), expecting function (T_FUNCTION) in</p>