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
)

    Excellent thanks, i had no idea that functione existed.. I'll check it out and use it as a last resort (ack, i dont have access to PHP5 either). In the mean time, is there any hope for my function? i would really like to make this one work if possible.

    Thanks again
    -emrys

      At the same link there is an example how somebdy did it under php 4

      <?php
      if (!function_exists('array_walk_recursive'))
      {
      function array_walk_recursive(&$input, $funcname, $userdata = "")
      {
      if (!is_callable($funcname))
      {
      return false;
      }

         if (!is_array($input))
         {
             return false;
         }
      
         foreach ($input AS $key => $value)
         {
             if (is_array($input[$key]))
             {
                 array_walk_recursive($input[$key], $funcname, $userdata);
             }
             else
             {
                 $saved_value = $value;
                 if (!empty($userdata))
                 {
                     $funcname($value, $key, $userdata);
                 }
                 else
                 {
                     $funcname($value, $key);
                 }
      
                 if ($value != $saved_value)
                 {
                     $input[$key] = $value;
                 }
             }
         }
         return true;

      }
      }
      ?>

        I saw that and tried to impliment it, but due to my need for it to return/keep track of an error array, i abandoned it...

        Anyway i made a bit of progress with my original function (shown below). I solved the problem of it just saying "Array" in the output by not letting the error checking run if $value is an array. Makes sense. Now it outputs the proper array and even does the error checking correclty.
        The problem now lies that the output subarrays dont reflect any changes made to them. If the value is not in a sub array it will make the corrections just fine though, so i'm guessing the problem lies with writing the changes to the subarray back to the master array... How can i make sure that when the subarray is passed recursively it is not a new array but the address of the subarray in the masterarray? its like it cant keep track of the pointers once it starts being called recursively...

        Let me know if i can clarify anything,
        Thanks in advance
        -emrys

        <?php
        function checkErrors(&$array)
        {           
        $errors = array(); foreach($array as $key => $value) { if(is_array($value)) { $errors = array_merge($errors,checkErrors($value)); } else { $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; } ?>

          Okay! I got it. The problem was that the foreach statement was creating a copy of the subarray in $value and i was passing the copy ($value) and not the original array ($array[$key]). Okay so i'll post the working function below.
          This is a useful function because it will go through a multidimensional array, do key specific error checking, keep track of the errors and do clean up on any var that you need.
          vattila, thanks for pointing out array_walk_recursive function, i can definetly make use of it.

          Thanks again,
          -emrys

          <?php
          function checkErrors(&$array)
          {           
          $errors = array(); foreach($array as $key => $value) { if(is_array($value)) $errors = array_merge($errors,checkErrors($array[$key])); else { $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; } ?>
            Write a Reply...