Easy way to apply a function to an array

Posted by alex on Stack Overflow See other posts from Stack Overflow or by alex
Published on 2010-02-16T02:36:36Z Indexed on 2010/05/25 23:31 UTC
Read the original article Hit count: 152

Filed under:
|
|

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, 'mysql_real_escape_string');

Warning: mysql_real_escape_string() expects parameter 2 to be resource, string given.

So I went with this slightly more ugly version

foreach($_POST as $key => $value) {
    $_POST[$key] = mysql_real_escape_string($value);
}

So why didn't the first way work? What is the best way to map values of an array to a function?

© Stack Overflow or respective owner

Related posts about php

Related posts about php5