On my webform, there are two kinds of address-fields a user can give. But since it's a complex webform (Drupal) it's kind of hard to achieve. Every time a user adds an extra address, the form is regenerated and there will be a new item added to the $address_afl
and $address_bev
-array. Is it possible to compress this code so the foreach-loop starts looping with the highest value (either $address_afl
or$address_bev
)
$address_afl = $form['field_afl_dienst_adressen']['und'];
$i = 0;
foreach($address_afl as $value) {
if (is_array($value)) {
$test = array_keys($value);
if(is_array($test)) {
foreach ($test as $z) {
if (strpos($z, '#') === 0) continue;
if (isset($form['field_afl_dienst_adressen']['und'][$i][$z])) {
$form['field_afl_dienst_adressen']['und'][$i][$z]['#prefix'] = "<div class='address-publicatie-$z'>";
$form['field_afl_dienst_adressen']['und'][$i][$z]['#suffix'] = "</div>";
}
}
}
}
$i++;
}
$address_bev = $form['field_bevoegde_adressen']['und'];
$i = 0;
foreach($address_bev as $value) {
if (is_array($value)) {
$test = array_keys($value);
if(is_array($test)) {
foreach ($test as $z) {
if (strpos($z, '#') === 0) continue;
if (isset($form['field_afl_dienst_adressen']['und'][$i][$z])) {
$form['field_bevoegde_adressen']['und'][$i][$z]['#prefix'] = "<div class='address-publicatie-$z'>";
$form['field_bevoegde_adressen']['und'][$i][$z]['#suffix'] = "</div>";
}
}
}
}
$i++;
}
if
is should not be at the same level as the first. However, array_keys always returns an array so you don't need the secondif
and they should be deleted. \$\endgroup\$