Hello everyone, I'm getting a little frustrated with this problem. Below i have provided sample input, the function that is called and the resulting sample output.. The function checkErrors() should take in an array check it for errors, do some maintenenance, like scrub the data and make certain things lowercase etc. then return an array of the errors found.
Due to the multidimensional array input i tried to make it recursive: when it finds a sub array it tries to call it self again and check for errors/do maintenence on the sub array. However, the fact that it does two things (return error array and make changes to the data) i had to make it pass by reference.
Anyway, as you can see from the output, it doesnt handle the sub arrays well. I believe the problem lies in keeping track of the changes made to the sub array...
Anyway, any and all help is appreciated,
thanks in advance
-emrys
Input:
Array
(
[name] => John Doe
[email] => johndoe@earthlink.net
[addresses] => Array
(
[1] => Array
(
[address] => 444 South Street #645
[city] => Podunk
[state] => CA
[zip] => 51103
[id] => 1
)
[2] => Array
(
[address] => 444 South Street #645
[city] => Podunk
[state] => CA
[zip] => 51103
[id] => 2
)
)
)
Function:
<?php
function checkErrors(&$array)
{
$errors = array();
foreach($array as $key => $value)
{
if(is_array($value))
$errors = array_merge($errors,checkErrors($value));
$array[$key] = scrub($value); //remove all questionable characters
if(strstr($key,'email') && !validEmail($value))
$errors[$key] = "Invalid $key";
elseif(strstr($key,'email'))
$array[$key] = strtolower($value); // makes email lowercase
if(strstr($key,'address') && poBoxCheck($value) && $key != "contactaddress")
$errors[$key] = "Invalid $key. Address cannot be a PO Box.";
if(strstr($key,'state') && strlen($value) != 2)
$errors[$key] = "";
elseif(strstr($key,'state'))
$array[$key] = strtoupper($value); // make all states upper case. ie. ca->CA
if(strstr($key,'zip') && !validZipCode($value))
$errors[$key] = "Invalid $key";
if(strlen($value) == 0) //mark all blank fields with a messageless error and make one error message
{
$errors[$key] = "";
$errors[blanks] = "Please do not leave any required fields blank. Required fields are marked below in red.";
}
}
return $errors;
}
?>
Output:
Array
(
[name] => John Doe
[email] => johndoe@earthlink.net
[addresses] => Array
)