Easy way to apply a function to an array

前端 未结 7 791
無奈伤痛
無奈伤痛 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:51

    It's because the mysql_real_escape_string is being given two parameters, both string.

    http://php.net/manual/en/function.mysql-real-escape-string.php

    http://www.phpbuilder.com/manual/en/function.array-map.php:

    array_map() returns an array containing all the elements of arr1 after applying the callback function to each one. The number of parameters that the callback function accepts should match the number of arrays passed to the array_map()

    you could do

    function myescape($val)
    {
        return mysql_real_escape_string($val);
    }
    

    ... then

    array_walk($_POST, 'myescape');
    

提交回复
热议问题