php foreach循环在函数名中使用变量

发布于 2025-01-07 00:08:41 字数 623 浏览 1 评论 0原文

我有一个 foreach 循环,为每个数组项创建一个函数。我需要为创建的每个函数指定一个唯一的名称,并且希望在函数名称中包含当前的数组项,但不知道如何执行此操作。

目前我有一个名为“function ttm_global__shortcode()”的函数,但我希望它是“function ttm_global_$acf_field_shortcode()”

$acf_fields = array("telephone_number", "fax_number", "email_address", "skype");

foreach($acf_fields as $acf_field) {    
    function ttm_global__shortcode() {
        ob_start();
        echo get_field($acf_field, 'options');
        $output = ob_get_contents();
        ob_end_clean();
        return $output; 
    }
    add_shortcode($acf_field, 'ttm_global_'.$acf_field.'_shortcode');
}

谢谢

I have a foreach loop that creates a function for each array item. I need a unique name for each function created and would like to include the current array item in the function name but don't know how to do this.

At the moment I have the function named as "function ttm_global__shortcode()" however I would like it to be "function ttm_global_$acf_field_shortcode()"

$acf_fields = array("telephone_number", "fax_number", "email_address", "skype");

foreach($acf_fields as $acf_field) {    
    function ttm_global__shortcode() {
        ob_start();
        echo get_field($acf_field, 'options');
        $output = ob_get_contents();
        ob_end_clean();
        return $output; 
    }
    add_shortcode($acf_field, 'ttm_global_'.$acf_field.'_shortcode');
}

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青春如此纠结 2025-01-14 00:08:41

使用 PHP 5.3 这可以通过匿名函数来完成

foreach($acf_fields as $acf_field) {
    add_shortcode($acf_field, function() use($acl_field) {
        // ...
    });
}

Using PHP 5.3 this can be done with anonymous functions

foreach($acf_fields as $acf_field) {
    add_shortcode($acf_field, function() use($acl_field) {
        // ...
    });
}
云仙小弟 2025-01-14 00:08:41

如果 add_shortcode 的第二个参数是回调,我认为您可以在不创建此类函数的情况下完成此操作。

$output = get_field($acf_field, 'options');
add_shortcode($acf_field, create_function('',"return '$output';"));

如果你还想创建函数。

PHP 5.3.0 之前

使用 create_function

foreach($acf_fields as $acf_field)
{  
    $funcs[$acf_field] = create_function('$acf_field', '
        ob_start();
        echo get_field($acf_field, 'options');
        $output = ob_get_contents();
        ob_end_clean();
        return $output;'
    );

 ....
 }

PHP 5.3.0 之后

foreach($acf_fields as $acf_field)
{  
    $funcs[$acf_field] = function($acf_field){
        ob_start();
        echo get_field($acf_field, 'options');
        $output = ob_get_contents();
        ob_end_clean();
        return $output;
    };

 ....
 }

注意: 增加了一个参数到你的函数。

If the second parameter of add_shortcode is a call back I think you can do it without creating such function.

$output = get_field($acf_field, 'options');
add_shortcode($acf_field, create_function('',"return '$output';"));

If you still want to create function.

Before PHP 5.3.0

Use create_function

foreach($acf_fields as $acf_field)
{  
    $funcs[$acf_field] = create_function('$acf_field', '
        ob_start();
        echo get_field($acf_field, 'options');
        $output = ob_get_contents();
        ob_end_clean();
        return $output;'
    );

 ....
 }

After PHP 5.3.0

foreach($acf_fields as $acf_field)
{  
    $funcs[$acf_field] = function($acf_field){
        ob_start();
        echo get_field($acf_field, 'options');
        $output = ob_get_contents();
        ob_end_clean();
        return $output;
    };

 ....
 }

Note: A parameter is added to your function.

小猫一只 2025-01-14 00:08:41

您可以使用 create_function 方法根据传递的参数创建匿名函数,并为其返回唯一的名称。

<?php
function process($var1, $var2, $farr)
{
    foreach ($farr as $f) {
        echo $f($var1, $var2) . "\n";
    }
}

// create a bunch of math functions
$f1 = 'if ($a >=0) {return "b*a^2 = ".$b*sqrt($a);} else {return false;}';
$f2 = "return \"min(b^2+a, a^2,b) = \".min(\$a*\$a+\$b,\$b*\$b+\$a);";
$f3 = 'if ($a > 0 && $b != 0) {return "ln(a)/b = ".log($a)/$b; } else { return false; }';
$farr = array(
    create_function('$x,$y', 'return "some trig: ".(sin($x) + $x*cos($y));'),
    create_function('$x,$y', 'return "a hypotenuse: ".sqrt($x*$x + $y*$y);'),
    create_function('$a,$b', $f1),
    create_function('$a,$b', $f2),
    create_function('$a,$b', $f3)
    );

echo "\nUsing the first array of anonymous functions\n";
echo "parameters: 2.3445, M_PI\n";
process(2.3445, M_PI, $farr);

// now make a bunch of string processing functions
$garr = array(
    create_function('$b,$a', 'if (strncmp($a, $b, 3) == 0) return "** \"$a\" '.
    'and \"$b\"\n** Look the same to me! (looking at the first 3 chars)";'),
    create_function('$a,$b', '; return "CRCs: " . crc32($a) . ", ".crc32($b);'),
    create_function('$a,$b', '; return "similar(a,b) = " . similar_text($a, $b, &$p) . "($p%)";')
    );
echo "\nUsing the second array of anonymous functions\n";
process("Twas brilling and the slithy toves", "Twas the night", $garr);
?>

查看 http://us.php.net/manual/en/function .create-function.php 了解更多详细信息。

You can use the create_function method to creates an anonymous function from the parameters passed, and returns a unique name for it.

<?php
function process($var1, $var2, $farr)
{
    foreach ($farr as $f) {
        echo $f($var1, $var2) . "\n";
    }
}

// create a bunch of math functions
$f1 = 'if ($a >=0) {return "b*a^2 = ".$b*sqrt($a);} else {return false;}';
$f2 = "return \"min(b^2+a, a^2,b) = \".min(\$a*\$a+\$b,\$b*\$b+\$a);";
$f3 = 'if ($a > 0 && $b != 0) {return "ln(a)/b = ".log($a)/$b; } else { return false; }';
$farr = array(
    create_function('$x,$y', 'return "some trig: ".(sin($x) + $x*cos($y));'),
    create_function('$x,$y', 'return "a hypotenuse: ".sqrt($x*$x + $y*$y);'),
    create_function('$a,$b', $f1),
    create_function('$a,$b', $f2),
    create_function('$a,$b', $f3)
    );

echo "\nUsing the first array of anonymous functions\n";
echo "parameters: 2.3445, M_PI\n";
process(2.3445, M_PI, $farr);

// now make a bunch of string processing functions
$garr = array(
    create_function('$b,$a', 'if (strncmp($a, $b, 3) == 0) return "** \"$a\" '.
    'and \"$b\"\n** Look the same to me! (looking at the first 3 chars)";'),
    create_function('$a,$b', '; return "CRCs: " . crc32($a) . ", ".crc32($b);'),
    create_function('$a,$b', '; return "similar(a,b) = " . similar_text($a, $b, &$p) . "($p%)";')
    );
echo "\nUsing the second array of anonymous functions\n";
process("Twas brilling and the slithy toves", "Twas the night", $garr);
?>

check out http://us.php.net/manual/en/function.create-function.php for more details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文