0

问题:

创建一个遍历三维数组的递归数组函数,并根据下面的格式打印出内容。如期望的输出/目标所示,不同问题的代码是相同的。

数组:

    $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)'
                        )
                    )
    );

期望的输出/目标:

        <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>
4

1 回答 1

0

看看这是否有帮助:

<?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;
?>

干杯!

于 2012-04-06T05:59:34.850 回答