Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

The problem:

To create a recursive array function that walks through a three dimensional array and print out the content according to the format below. As illustrated in the desired output/goal, the code for the different questions is the same.

The array:

    $items = array (
                    'What is your gender?' => array 
                    ( 
                        'gender' => array 
                        (
                            '1' => 'Man', 
                            '2' => 'Woman'
                        )
                    ),

                    'What is your education?' => array 
                    (
                        'education' => array 
                        (   
                            '1' => 'Elementary school', 
                            '2' => 'Middle school', 
                            '3' => 'High school', 
                            '4' => 'Post-secondary education (BSc, MSc)', 
                            '5' => 'Advanced education (PhD)'
                        )
                    )
    );

Desired output/goal:

        <div class="control-group">
            <label class="control-label"><strong>What is your gender?</strong></label>
            <div class="controls">
                <label class="radio">
                    <input type="radio" name="gender" id="gender" value="1" checked>
                    Man
                </label>

                <label class="radio">
                    <input type="radio" name="gender" id="gender" value="2">
                    Woman
                </label>
            </div>
        </div>

        <div class="control-group">
            <label class="control-label"><strong>What is your education?</strong></label>
            <div class="controls">
                <label class="radio">
                    <input type="radio" name="education" id="education" value="1" checked>
                    Elementary school
                </label>

                <label class="radio">
                    <input type="radio" name="education" id="education" value="2">
                    Middle school
                </label>

                <label class="radio">
                    <input type="radio" name="education" id="education" value="3">
                    High school
                </label>

                <label class="radio">
                    <input type="radio" name="education" id="education" value="4">
                    Post-secondary education (BSc, MSc)
                </label>

                <label class="radio">
                    <input type="radio" name="education" id="education" value="5">
                    Advanced education (PhD)
                </label>
            </div>
        </div>
share|improve this question
1  
What have you tried so far? – Amber Apr 6 '12 at 5:15
I have tried to work on this but I'm missing something important which I can't figure out: pastebin.com/ZJRMga0F – kexxcream Apr 6 '12 at 5:21

1 Answer

up vote 0 down vote accepted

See if this helps:

<?php
$items = array (
    'What is your gender?' => array ( 
        'gender' => array (
            '1' => 'Man', 
            '2' => 'Woman'
        )
    ),
    'What is your education?' => array (
        'education' => array (   
            '1' => 'Elementary school', 
            '2' => 'Middle school', 
            '3' => 'High school', 
            '4' => 'Post-secondary education (BSc, MSc)', 
            '5' => 'Advanced education (PhD)'
        )
    )
);

function buildHTML( $items, $is_root=true ) {
    $str = '';

    if( $is_root ) {
        foreach( $items as $key => $value ) {
            $str .= '<div class="control-group"><label class="control-label"><strong>' . $key . '</strong></label>';

            if( is_array( $value ) ) {
                $str .= '<div class="controls">';
                $str .= buildHTML( $value, false );
                $str .= '</div>';
            }
        }
    } else {
        $first = 0;
        foreach( $items as $key => $value ) {
            foreach( $value as $key1 => $value1 ) {
                if( ! $first++ ) {
                    $checked = 'checked="checked"';
                } else {
                    $checked = '';
                }
                $str .= '<label class="radio"><input type="radio" name="' . $key . '" id="' . $key . '" value="' . $key1 . '" ' . $checked . '>' . $value1 . '</label>';
            }
        }
    }

    return $str;
}

$html = buildHTML( $items );

echo $html;
?>

Cheers!

share|improve this answer
Works perfect, made a small change by adding $str .= '</div>'; at the end of the first foreach. Now the function will produce identical code as my example. – kexxcream Apr 6 '12 at 6:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.