简体   繁体   中英

How do I create a function inside of a foreach loop php?

Theres this wordpress plugin called ninja forms, http://developer.ninjaforms.com/codex/merge-tags/

        /* Individual tag registration. */
        $this->merge_tags = array(

            'foo' => array(
              'id' => 'foo',
              'tag' => '{my:foo}', // The tag to be  used.
              'label' => __( 'Foo', 'my_plugin' ), // Translatable label for tag selection.
              'callback' => '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';

} }

Value of 'callback' is then used as a function, public function(foo).

I have added this to the array:

[foo] => Array
    (
        [id] => foo
        [tag] => {my:foo}
        [label] => Foo
        [callback] => foo
    )

[surveyid] => Array
    (
        [id] => surveyid
        [tag] => {my:surveyid}
        [label] => Surveyid
        [callback] => surveyid
    )

[membername] => Array
    (
        [id] => membername
        [tag] => {my:membername}
        [label] => Membername
        [callback] => membername
    )

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.

 /**
   * The callback method for the {my:foo} merge tag.
   * @return string
   */
  public function foo()
  {
    // Do stuff here.
    return 'bar';
  }

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.

This is what I have.

        $data = array(

        '@attributes' => array(
            'surveyid' => 'V54236',
            'membername' => 'John Smith',

            ));

    $realThing = array();
    foreach($data['@attributes'] as $key => $value) {    
        $realThing[$key] = array(
            'id' =>  $key,
            'tag' => '{my:'.$key.'}', 
            'label' => __( ucfirst($key), 'my_plugin' ),
            'callback' => $key

   );
}

    $this->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. */ }

    }

My attempt to assign functions for each callback value.

        foreach($realThing as $key => $value){
             public function $key['callback'](){
                 return $data['@attributes'][$key];
             }
         };

desired output:

  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';

All help appreciated.

also getting: syntax error, unexpected 'foreach' (T_FOREACH), expecting function (T_FUNCTION) in

You have some mistakes in handling the data and you should declare each function only once:

foreach($realThing as $key => $value){
         if(!function_exists($value['callback'])){
             public function $value['callback'](){
                 return $data['@attributes'][$key];
             }
          }
 };

However this code doesn't work because a variable isn't allowed in a function declaration. The following code should work, but you have to call the callback differently as it is stored in an array:

$callbacks = array();
foreach($realThing as $key => $value){
         if(!isset($callbacks[$value['callback']])){
             $callbacks[$value['callback']] = function () use ($data, $key){
                 return $data['@attributes'][$key];
             };
          }
 };
 unset($key);

 echo $callbacks["surveyid"]();

I'm pretty sure though you could do what you want to do in some other way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM