array_map不在类中工作

时间:2011-03-24 16:18:32

标签: php arrays class function array-map

我正在尝试创建一个处理数组的类,但我似乎无法让array_map()在其中工作。

<?php
//Create the test array
$array = array(1,2,3,4,5,6,7,8,9,10);
//create the test class
class test {
//variable to save array inside class
public $classarray;

//function to call array_map function with the given array
public function adding($data) {
    $this->classarray = array_map($this->dash(), $data);
}

// dash function to add a - to both sides of the number of the input array
public function dash($item) {
    $item2 = '-' . $item . '-';
    return $item2;
}

}
// dumps start array
var_dump($array);
//adds line
echo '<br />';
//creates class object
$test = new test();
//classes function adding
$test->adding($array);
// should output the array with values -1-,-2-,-3-,-4-... 
var_dump($test->classarray);

此输出

array(10) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) [5]=> int(6) [6]=> int(7) [7]=> int(8) [8]=> int(9) [9]=> int(10) }

Warning: Missing argument 1 for test::dash(), called in D:\xampp\htdocs\trainingdvd\arraytesting.php on line 11 and defined in D:\xampp\htdocs\trainingdvd\arraytesting.php on line 15

Warning: array_map() expects parameter 1 to be a valid callback, function '--' not found or invalid function name in D:\xampp\htdocs\trainingdvd\arraytesting.php on line 11 NULL

我做错了什么或者这个功能在课堂内不起作用?

5 个答案:

答案 0 :(得分:117)

您以错误的方式指定dash作为回调。

这不起作用:

$this->classarray = array_map($this->dash(), $data);

这样做:

$this->classarray = array_map(array($this, 'dash'), $data);

了解回调可能采用的不同形式here

答案 1 :(得分:24)

你好你可以使用像这样的

    // Static outside of class context
array_map( array( 'ClassName', 'methodName' ), $array );

// Static inside class context
array_map( array( __CLASS__, 'methodName' ), $array );

// Non-static outside of object context
array_map( array( $object, 'methodName' ), $array );

// Non-static inside of object context
array_map( array( $this, 'methodName' ), $array );

答案 2 :(得分:2)

array_map($this->dash(), $data)使用0个参数调用$this->dash()并使用返回值作为回调函数应用于数组的每个成员。您想要array_map(array($this,'dash'), $data)

答案 3 :(得分:1)

必须阅读

$this->classarray = array_map(array($this, 'dash'), $data);

array - 事物是对象实例方法的PHP callback。对常规函数的回调定义为包含函数名称('functionName')的简单字符串,而静态方法调用定义为array('ClassName, 'methodName')或类似于以下字符串:'ClassName::methodName'(这适用于PHP 5.2.3)。

答案 4 :(得分:0)

对于多维数组(任何数组):

    $data = array_map('decode'), $data);

    function decode($data)
    {
        if (is_array($data)) {
            foreach ($data as &$value) {
                if (is_array($value)) {
                    $value = decode($value);
                } else {
                    $value = html_entity_decode($value);
                }
            }
        } else {
            $data = html_entity_decode($data);
        }
        return $data;
    }

对于类中的多维数组(任何数组):

$data = array_map(array($this,'decode'), $data);

private function decode($data)
{
    if (is_array($data)) {
        foreach ($data as &$value) {
            if (is_array($value)) {
                $value = $this->decode($value);
            } else {
                $value = html_entity_decode($value);
            }
        }
    } else {
        $data = html_entity_decode($data);
    }
    return $data;
}