Jump to content

Clearing zero variables


ms.buggy

Recommended Posts

Hi, I have a form with multiple fields that are stored to database.

Problem is that for the further purposes, 0 values are invalid. So if someone writes zero values programn should clear those.

 

Ok, this is very simple made with multiple if-functions, like 'if($variable==0){$variable="";}'.

 

But I'm interested to find other way to handle multiple values, instead of writing separate if-function for all of them. I thought that this might not be the lightest way to handle variables. I think the optimal solution would be written so that zero values would not post from form to function that stores values to database.

Any suggestions?

 

 

Link to comment
https://forums.phpfreaks.com/topic/200983-clearing-zero-variables/
Share on other sites

As you want to test it on all the variable of post. Please find my code below :-

 

function clearZero($arr){
if(is_array($arr) && !empty($arr)){
	foreach($arr as $key=>$val){
		$val != 0 ? $new_arr[$key] = $val : false;
	}
}
return $new_arr;
}


$clearArr = clearZero($_POST);
print_r($clearArr);

Okay, code works mainly correctly. But I have managed to resieve an error message in some cases.

 

Message is: "Fatal error: Cannot redeclare clearzero() (previously declared in..." and it's pointing to the line2 'function clearZero($value)'.

 

This is caused when saved data already exist in the database, strange becouse there is no limitation/controlling of duplicate data. What is really strange is that this is caused when the function is not even used?? Becouse I have few values that are not even check with the 'clear zero' -funtion, when reading them to variables, and this may happen even if only these variables are used.

 

The function is:

function clearZero($value)
{
    return ($value==0) ? '' : trim($value);
}

 

And variables are read:

$variable1=trim($_POST['variable1']);
$variable2=clearZero($_POST["variable2"]);

 

I only guessing, but could this be some kind of a cache issue?

As you want to test it on all the variable of post. Please find my code below :-

 

function clearZero($arr){
if(is_array($arr) && !empty($arr)){
	foreach($arr as $key=>$val){
		$val != 0 ? $new_arr[$key] = $val : false;
	}
}
return $new_arr;
}


$clearArr = clearZero($_POST);
print_r($clearArr);

 

Actually you can do this better and faster using mjdamato's function.

 

$_POST = array_map('clearZero', $_POST);

Okay, code works mainly correctly. But I have managed to resieve an error message in some cases. Message is: "Fatal error: Cannot redeclare clearzero() (previously declared in..." and it's pointing to the line2 'function clearZero($value)'.

 

See: http://www.phpfreaks.com/tutorial/defining-a-php-function-only-once

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.