I have this array (see it below) and I want to repeat each array that has a key "repeat" with a value that represents how many times to repeat.
$fields = array(
array(
'type' => 'title-wrap',
'holder' => 'h4',
'heading' => 'Test heading',
),
array(
'repeat' => 3,
'type' => 'radio',
'name' => 'specific_name',
'value' => array(
0 => 'First', // value for first repeat
1 => 'Second', // value for second repeat
2 => 'Third' // value for third repeat
),
)
);
For that I have created a recursive function named generateForm
:
function generateForm($fields, $index = 0) {
if ( $fields == '' ) { return false; }
foreach ($fields as $field) {
if ( isset($field['type']) ) {
switch ( $field['type'] ) {
case 'title-wrap':
echo $field['heading'];
break;
case 'radio':
echo $field['value'][$index];
break;
}
}
if ( isset($field['repeat']) ) {
for ($i=0; $i < $field['repeat']-1; $i++) {
generateForm($field, $i);
}
}
}
}
The output I want:
Test heading
First
Second
Third
But I don't get the last two words in the output. What am I doing wrong?