验证数组

I would like to validate my form and set the given values back to the input fields.

This is what var_dump($_GET) returns when I send the form:

array(10) { 
    ["ich_vorname"]=> string(5) "sdfdf" 
    ["ich_name"]=> string(0) "" 
    ["ich_strasse_nr"]=> string(0) "" 
    ["ich_plz_ort"]=> string(0) "" 
    ["ich_email"]=> string(0) "" 
    ["ich_konto_nr"]=> string(0) "" 
    ["friend_vorname"]=> array(3) { 
        [0]=> string(0) "" 
        [1]=> string(0) "" 
        [2]=> string(0) "" 
    } 
    ["friend_name"]=> array(3) { 
        [0]=> string(0) "" 
        [1]=> string(0) "" 
        [2]=> string(0) "" 
    } 
    ["friend_strasse_nr"]=> array(3) { 
        [0]=> string(0) "" 
        [1]=> string(0) "" 
        [2]=> string(0) "" 
    } 
    ["friend_plz_ort"]=> array(3) { 
        [0]=> string(0) "" 
        [1]=> string(0) "" 
        [2]=> string(0) "" 
    } 
}

As you see there are some strings and some arrays. For the strings I am using the following code to find out if the value is set. If it is set, so I can use something like value="<?= $_SESSION['ich_vorname'] ?>".

$_SESSION['ich_vorname'] = (isset($_GET['ich_vorname']) && !empty($_GET['ich_vorname'])) ? $_GET['ich_vorname'] : 'error';
$_SESSION['ich_name'] = (isset($_GET['ich_name']) && !empty($_GET['ich_name'])) ? $_GET['ich_name'] : 'error';
$_SESSION['ich_strasse_nr'] = (isset($_GET['ich_strasse_nr']) && !empty($_GET['ich_strasse_nr'])) ? $_GET['ich_strasse_nr'] : 'error';
$_SESSION['ich_plz_ort'] = (isset($_GET['ich_plz_ort']) && !empty($_GET['ich_plz_ort'])) ? $_GET['ich_plz_ort'] : 'error';
$_SESSION['ich_email'] = (isset($_GET['ich_email']) && !empty($_GET['ich_email'])) ? $_GET['ich_email'] : 'error';
$_SESSION['ich_konto_nr'] = (isset($_GET['ich_konto_nr']) && !empty($_GET['ich_konto_nr'])) ? $_GET['ich_konto_nr'] : 'error';

The strings are not a problem but the arrays are! How can I set the value for the array fields when the fields are getting generated dynamically?

You can do something like this to validate the form inputs and store them in $_SESSION superglobal.

foreach($_GET as $key => $value){
    if(is_array($value)){
        $tmp_arr = array();
        foreach($value as $v){
            $v = trim($v);
            if(!empty($v)){
                $tmp_arr[] = $v;
            }else{
                $tmp_arr[] = "error";
            }
        }
        $_SESSION[$key] = $tmp_arr;
    }else{
        $value = trim($value);
        if(!empty($value)){
            $_SESSION[$key] = $value;
        }else{
            $_SESSION[$key] = "error";
        }
    }
}

var_dump($_SESSION);  // to display $_SESSION contents

Just loop through the arrays.

if (empty($_GET['friend_vorname'])) {
    $_SESSION['friend_vorname'] = 'error';
} else {
    $_SESSION['friend_vorname'] = array_filter($_GET['friend_vorname']);
}

Similar to other suggestions, however this feels cleaner:

$values = $_GET;
array_walk_recursive($values, function(&$value, $key){
    $value = !empty($value) ? $value : 'error';
});

Stores the $_GET array as $values and recursively iterates through that array. Then you can do what you want with $values, ie. assign them to $_SESSION.

You may also want to consider only validating the fields that are required by using some kind of "map" indicating which fields need to be validated.