I’m trying to replace empty field values with NULL, but can’t seem to figure out how to do it. I’ve tried array_map, array_filter, and array_walk, but to no avail. Example:
function replaceWithNull($var)
{
if (empty($var) || $var == ' ') {
$var = "NULL";
}
}
array_walk($_POST, "replaceWithNull");
Instead, it remains empty/blank. What am I missing?
You have to use references for argument passing in order to alter the elements in the array:
Otherwise you will be changing only a copy of the variable.
Read about it here: http://www.php.net/manual/en/functions.arguments.php