设置array_map php的范围

时间:2010-02-24 21:01:17

标签: php arrays recursion array-map

嘿所有,我不时使用array_map来编写递归方法。例如

function stripSlashesRecursive( $value ){

    $value = is_array($value) ?
        array_map( 'stripSlashesRecursive', $value) :
    stripslashes( $value );
    return $value;
}

问题:

说我想把这个函数放在一个静态类中,我如何使用array_map回到类中静态方法的范围,如Sanitize :: stripSlashesRecursive(); 我确定这很简单,但我不能把它想象出来,看看php.net。

3 个答案:

答案 0 :(得分:16)

当使用类方法作为array_map()usort()等函数的回调时,必须将回调作为双值数组发送。第二个值始终是作为字符串的方法名称。第一个值是上下文(类名或对象)

// 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 );

答案 1 :(得分:1)

array_map回调作为其第一个参数。

静态方法的回调如下所示:

array('classname', 'methodname')


这意味着,在您的具体情况下,您将使用:

array_map(array('stripSlashesRecursive', ''), $value);


有关回调的更多信息,请参阅PHP手册的这一部分:Pseudo-types and variables used in this documentation - callback

答案 2 :(得分:0)

array_map( array('Sanitize', 'stripSlashesRecursive'), $value) ...
相关问题