方法可以用作 array_map 函数吗

发布于 2024-07-25 21:05:00 字数 317 浏览 5 评论 0原文

我想做这样的事情:

class Cls {
  function fun($php) {
    return 'The rain in Spain.';
  }
}

$ar = array(1,2,3);
$instance = new Cls();
print_r(array_map('$instance->fun', $ar));
               // ^ this won't work

但是 array_map 的第一个参数应该是函数的名称。 我想避免在 $instance->fun 周围编写包装函数,但这似乎不可能。 真的吗?

I want to do something like this:

class Cls {
  function fun($php) {
    return 'The rain in Spain.';
  }
}

$ar = array(1,2,3);
$instance = new Cls();
print_r(array_map('$instance->fun', $ar));
               // ^ this won't work

but the first argument to array_map is supposed to be the name of the function. I want to avoid writing a wrapper function around $instance->fun, but it doesn't seem like that's possible. Is that true?

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

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

发布评论

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

评论(4

迎风吟唱 2024-08-01 21:05:01

它对我有用如下

<?php
class ExcelAutoFilterController extends Controller
{
    public function transpose($value):array
        {
            return [$value];
        }

        public function generateExcelDropdownDownload($file = 'helloWorld.xlsx')
        {

            $countries = [0 => "Algeria"1 => "Angola"2 => "Benin"3 => "Botswana"4 => "Burkina"5 => "Burundi"6 => "Cameroon"7 => "Cape Verde"8 => "Central African Republic"9 => "Chad"10 => "Comoros"11 => "Congo"12 => "Congo, Democratic Republic of"13 => "Djibouti"14 => "Egypt"15 => "Equatorial Guinea"16 => "Eritrea"17 => "Ethiopia"18 => "Gabon"19 => "Gambia"20 => "Ghana"21 => "Guinea"22 => "Guinea-Bissau"23 => "Ivory Coast"24 => "Kenya"25 => "Lesotho"26 => "Liberia"27 => "Libya"28 => "Madagascar"29 => "Malawi"30 => "Mali"31 => "Mauritania"32 => "Mauritius"];

            $countries = array_map('self::transpose', $countries);
        }
        

It worked for me as follows

<?php
class ExcelAutoFilterController extends Controller
{
    public function transpose($value):array
        {
            return [$value];
        }

        public function generateExcelDropdownDownload($file = 'helloWorld.xlsx')
        {

            $countries = [0 => "Algeria"1 => "Angola"2 => "Benin"3 => "Botswana"4 => "Burkina"5 => "Burundi"6 => "Cameroon"7 => "Cape Verde"8 => "Central African Republic"9 => "Chad"10 => "Comoros"11 => "Congo"12 => "Congo, Democratic Republic of"13 => "Djibouti"14 => "Egypt"15 => "Equatorial Guinea"16 => "Eritrea"17 => "Ethiopia"18 => "Gabon"19 => "Gambia"20 => "Ghana"21 => "Guinea"22 => "Guinea-Bissau"23 => "Ivory Coast"24 => "Kenya"25 => "Lesotho"26 => "Liberia"27 => "Libya"28 => "Madagascar"29 => "Malawi"30 => "Mali"31 => "Mauritania"32 => "Mauritius"];

            $countries = array_map('self::transpose', $countries);
        }
        
美人如玉 2024-08-01 21:05:00

是的,您可以对方法进行回调,如下所示:

array_map(array($instance, 'fun'), $ar)

请参阅回调类型< PHP 手册中的 /a> 了解更多信息

Yes, you can have callbacks to methods, like this:

array_map(array($instance, 'fun'), $ar)

see the callback type in PHP's manual for more info

岁月苍老的讽刺 2024-08-01 21:05:00

您还可以使用

array_map('Class::method', $array) 

语法。

You can also use

array_map('Class::method', $array) 

syntax.

维持三分热 2024-08-01 21:05:00

其实,你需要了解Callback的定义,请参考下面的代码:

<?php 

// An example callback function
function my_callback_function() {
    echo 'hello world!';
}

// An example callback method
class MyClass {
    static function myCallbackMethod() {
        echo 'Hello World!';
    }
}

$myArray = [1, 2, 3, 4];

// Type 1: Simple callback
array_map('my_callback_function', $myArray); 

// Type 2: Static class method call
array_map(array('MyClass', 'myCallbackMethod'), $myArray); 

// Type 3: Object method call
$obj = new MyClass();
array_map(array($obj, 'myCallbackMethod'), $myArray);

// Type 4: Static class method call (As of PHP 5.2.3)
array_map('MyClass::myCallbackMethod', $myArray);

// Type 5: Relative static class method call (As of PHP 5.3.0)
class A {
    public static function who() {
        echo "A\n";
    }
}

class B extends A {
    public static function who() {
        echo "B\n";
    }
}

array_map(array('B', 'parent::who'), $myArray); // A
?>

来自: http://php.net/manual/en/language.types.callable.php

Actually, you need to know the definition of Callback, please kindly refer to the following code:

<?php 

// An example callback function
function my_callback_function() {
    echo 'hello world!';
}

// An example callback method
class MyClass {
    static function myCallbackMethod() {
        echo 'Hello World!';
    }
}

$myArray = [1, 2, 3, 4];

// Type 1: Simple callback
array_map('my_callback_function', $myArray); 

// Type 2: Static class method call
array_map(array('MyClass', 'myCallbackMethod'), $myArray); 

// Type 3: Object method call
$obj = new MyClass();
array_map(array($obj, 'myCallbackMethod'), $myArray);

// Type 4: Static class method call (As of PHP 5.2.3)
array_map('MyClass::myCallbackMethod', $myArray);

// Type 5: Relative static class method call (As of PHP 5.3.0)
class A {
    public static function who() {
        echo "A\n";
    }
}

class B extends A {
    public static function who() {
        echo "B\n";
    }
}

array_map(array('B', 'parent::who'), $myArray); // A
?>

From: http://php.net/manual/en/language.types.callable.php

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