Cant seem to append to array in php
<p>This simple script should theoretically check the form for errors and then print any errors it finds.</p>

<p>The formValidate function takes in a form array, each field in the form has a value which is validated. The field also has an errors key whose value is an empty array. I am trying to append any errors I find to the errors array of the particular field. I then return the form array when I am done.</p>

<p>I later print out all the errors for the fields. Unfortunately the errors never show up.</p>

<p>I have been breaking my head over this for many hours now and I cant for the life of me figure out why the form errors in my script are not appeding.</p>

<p>Any help would be greatly appreciated!</p>

<pre><code># get value from array
function array_get(array $array, $key, $default = null)
{
    return (array_key_exists($key, $array)) ? $array[$key] : $default;
}

# get string value from array
function array_get_string(array $array, $key, $default = '', $trim = true)
{
    $val = array_get($array, $key);
    if (is_string($val)) {
        return ($trim) ? trim($val) : $val;
    }
    return $default;
}

function validateForm($form)
{
    // validateField each field
    foreach ($form as $field)
    {
        foreach ($field['validation'] as $validate)
        {
            switch($validate)
            {
                case 'email':
                    if(!filter_var($field['value'], FILTER_VALIDATE_EMAIL)) {
                        $field['errors'][] = $field['value'] . ' is an invalid email address.';
                    }
                break;
                case 'number':
                    if(!preg_match('/^[0-9 ]+$/', $field['value'])) {
                        $field['errors'][] = $field['value'] . ' is an invalid number.';
                    }
                break;
                case 'alpha':
                    if(!preg_match('/^[a-zA-Z ]+$/', $field['value'])) {
                        $field['errors'][] = $field['value'] . ' contains invalid characters. This field only accepts letters and spaces.';
                    }
                break;
            }
        }
    }

    return $form;
}

// $post = filter_input_array( INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS );
$post = $_POST;
$ajax = array_get_string($post, "request_method") == 'ajax';

# select data that needs validation
$form = array(
    'fullname' =&gt; array(
        'value' =&gt; array_get_string($post, "full-name"),
        'validation' =&gt; array('alpha', 'required'),
        'errors' =&gt; array(),
    ),
    'emailaddress' =&gt; array(
        'value' =&gt; array_get_string($post, "email-address"),
        'validation' =&gt; array('email'),
        'errors' =&gt; array(),
    ),
    'activites' =&gt; array(
        'value' =&gt; array_get_string($post, "activites"),
        'validation' =&gt; array('default'),
        'errors' =&gt; array(),
    ),
    'country' =&gt; array(
        'value' =&gt; array_get_string($post, "country"),
        'validation' =&gt; array('default'),
        'errors' =&gt; array(),
    ),
    'contactpreference' =&gt; array(
        'value' =&gt; array_get_string($post, "contact-preference"),
        'validation' =&gt; array('default'),
        'errors' =&gt; array(),
    ),
    'message' =&gt; array(
        'value' =&gt; array_get_string($post, "message"),
        'validation' =&gt; array('alpha'),
        'errors' =&gt; array(),
    ),
);

// validate the form
$form = validateForm($form);

foreach ($form as $field)
{
    foreach ($field['errors'] as $error)
    {
        echo $error . '&lt;br /&gt;';
    }
}
</code></pre>
