Easy way to apply a function to an array

前端 未结 7 824
無奈伤痛
無奈伤痛 2020-12-03 14:31

I am aware of array_walk() and array_map(). However when using the former like so (on an old project) it failed

array_walk($_POST,          


        
7条回答
  •  忘掉有多难
    2020-12-03 14:58

    http://php.net/manual/en/function.array-walk.php says that array_walk will call the function with 2 arguments, the value and the key. You should write a new function to wrap mysql_real_escape_string. Something like:

    function wrapper($val, $key){
        return mysql_real_escape_string($val);
    }
    

    And then:

    array_walk($_POST, 'wrapper');
    

    Sorry if my PHP is not correct, but I think you'll catch the general idea.

提交回复
热议问题