array_map not working in classes

后端 未结 4 1143
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-24 01:00

I am trying to create a class to handle arrays but I can\'t seem to get array_map() to work in it.



        
相关标签:
4条回答
  • 2020-12-24 01:18

    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.

    0 讨论(0)
  • 2020-12-24 01:20

    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).

    0 讨论(0)
  • 2020-12-24 01:40

    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.

    0 讨论(0)
  • 2020-12-24 01:41

    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 );
    
    0 讨论(0)
提交回复
热议问题