set_from_array($array)

if $array is not an array: return false

Attempts to set class variables with names coinciding with the array indexes and values coinciding with their respective values.

If the method set_"array_index" does not exist: return false

The value must meet the variable setter validation requirements. The function will create an array of variable names that fail to meet the requirements. If this array exists (there are errors), return $error_array

If there were no issues return null

function code:
<?php
    function set_from_array($array = '')
    {
        $errors = null;
        if (@!is_array($array))
            return false;
        foreach($array as $k=>$v)
        {
            if (@method_exists($this, "set_$k"))
            {
                if (@!$this->{"set_$k"}($v))
                    $errors[] = $this->{$k."_label"};
            }
            else
                return false;
        }
        return $errors;
    }
?>



Example:

example.php
<?php
include("lib.php");
load("person");

if ($array = $_POST["array"])
{
    $p = new person;
    if ($errors = $p->set_from_array_unsafe($array))
    {
        echo "Error. The following fields are invalid: ";
        echo implode(", ", $errors);
        echo ".";
        return 0;
    }
    else
    {
        ...
    }
}

?>


<form method=post action=example.php>
Name:<input type=text name=array[name]><br>
Address:<input type=text name=array[address]><br>
City:<input type=text name=array[city]><br>
State:<input type=text name=array[state]><br>
Age:<input type=text name=array[age]><br>
Birthdate:<input type=text name=array[birthdate]><br>
Mother:<input type=text name=array[mother]><br>
Father:<input type=text name=array[father]><br>
<input type=submit value=Submit>
</form>