php array_map 在类中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5422242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
array_map not working in classes
提问by Justin
I am trying to create a class to handle arrays but I can't seem to get array_map()
to work in it.
我正在尝试创建一个类来处理数组,但我似乎无法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);
This outputs
这输出
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) }
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: 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
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
What am I doing wrong or does this function just not work inside classes?
我做错了什么或者这个功能在类中不起作用?
回答by Jon
回答by Vijaysinh Parmar
Hello You can use Like this one
你好你可以使用像这个
// 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 );
回答by Anomie
array_map($this->dash(), $data)
calls $this->dash()
with 0 arguments and uses the return value as the callback function to apply to each member of the array. You want array_map(array($this,'dash'), $data)
instead.
array_map($this->dash(), $data)
$this->dash()
使用 0 个参数调用并使用返回值作为回调函数应用于数组的每个成员。你想要array_map(array($this,'dash'), $data)
。
回答by Stefan Gehrig
It must read
它必须读
$this->classarray = array_map(array($this, 'dash'), $data);
The array
-thing is the PHP callbackfor a object instance method. Callbacks to regular functions are defined as simple strings containing the function name ('functionName'
), while static method calls are defined as array('ClassName, 'methodName')
or as a string like that: 'ClassName::methodName'
(this works as of PHP 5.2.3).
该array
-thing是PHP回调的对象实例的方法。对常规函数的回调被定义为包含函数名称 ( 'functionName'
) 的简单字符串,而静态方法调用被定义为array('ClassName, 'methodName')
或作为这样的字符串:('ClassName::methodName'
从 PHP 5.2.3 开始有效)。