从数组中检索值

时间:2011-03-27 14:09:43

标签: php

你好编码器我有一个.csv格式的数据库。为了检索数据,我使用了csv解析器从我得到结果的数据

 Array
(
    [fieldset_name] => Personal Details
    [field_name] => applicant_name
    [field_label] => Your Name
    [field_type] => text
    [css_classes] => required
    [minlength] => 4
    [maxlength] => 10
    [default_value] => 
    [help_text] => 
)

        Array
(
    [fieldset_name] => Personal Details
    [field_name] => applicant_address
    [field_label] => Address
    [field_type] => textarea
    [css_classes] => required
    [minlength] => 4
    [maxlength] => 10
    [default_value] => 
    [help_text] => 
)

现在我使用这段代码使这个数组像html格式一样

<?php foreach ( $form_field as $key => $v1 ) {
         $fieldset_name = $v1['fieldset_name'];
          $field_name = $v1['field_name'];
          $field_label = $v1['field_label'];
         $field_type = $v1['field_type'];
          $css_classes = $v1['css_classes'];
          $minlength = $v1['minlength'];
          $maxlength = $v1['maxlength'];
          $default_value = $v1['default_value'];
 <label for "<?php echo $field_name; ?>"></label><?php echo $field_label; ?><input type="<?php echo $field_type;?>" id = "<?php echo $css_classes; ?>"/>

        <?php  } 

          ?>

从我得到结果

<label for "applicant_name"></label>Your Name<input type="text" id = "required"/> 
<label for "city"></label>City<input type="text" id = "required"/> 

现在我想设置两个值。首先我想设置像

这样的值
<legend>Your Name</legend>
<label for "applicant_name"></label>Your Name<input type="text" id = "required"/> 
<legend>Address</legend>
<label for "city"></label>City<input type="text" id = "required"/>

所以请告诉我如何在foreach循环或if..else条件下执行此操作,因为我有一个像这样的大表单,所以我不能手动设置值。

2 个答案:

答案 0 :(得分:1)

id preoperty应具有唯一值,因为这是标签的for属性所指向的。

您将“名称”和“城市”写入您的HTML代码,但无法从您的数组中生成它们。

您可以获得最接近期望结果的是:

foreach ($form_field as $f) {
    printf ('<label for="%s">%s</label><input type="text" class="%s" name="%s" value="%s"/>',
        $f['field_name'], 
        $f['field_label'],
        $f['css_classes'],
        $f['field_name'],
        $f['default_value']
    );
}

这会产生类似的东西:

<label for="applicant_name">Your Name</label><input type="text" class="required" name="applicant_name" value=""/>
<label for="applicant_address">Address</label><input type="text" class="required" name="applicant_address" value=""/>

答案 1 :(得分:0)

您可以使用正则表达式 - 例如:

$legend = preg_replace('/(<.+label>(.+?)<input)/g',"<legend>$2</legend>\r\n$1",$source)