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
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);

Link to comment
Share on other sites

Thank you very much mjdamato!

 

And thank you too phpchamps, but was this only for testing?  In this case, I prefer to test and check the status of variables from the database, just in case.

 

It always nice to learn new!

Link to comment
Share on other sites

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?

Link to comment
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);

 

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

 

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.