array_map not working in classes
Solution 1:
You are specifying dash
as the callback in the wrong way.
This does not work:
$this->classarray = array_map($this->dash(), $data);
This does:
$this->classarray = array_map(array($this, 'dash'), $data);
Read about the different forms a callback may take here.
Solution 2:
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 );
Solution 3:
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.
Solution 4:
It must read
$this->classarray = array_map(array($this, 'dash'), $data);
The array
-thing is the PHP callback for 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).