【问题标题】:foreach echo into 2 different input from jsonforeach 回显到来自 json 的 2 个不同输入
【发布时间】:2019-03-07 12:50:45
【问题描述】:

我被困在这一点上,我希望:

对于每个不包含“+price”的名称,将值回显到输入 class="optionname" 对于每个包含“+price”的名称,将值回显到未输入的 class="optionprix"

我的 php 和 html :

foreach($fields as $field):

<input type="text" class="optionname" name="<?php echo $field['name']; ?>" value="<?php echo $field['value']; ?>"/>

Prix : <input type="text" class="optionprix" name="<?php echo $field['name'].'+price'; ?>" value="<?php echo $optionprix; ?>"

endforeach;

我的记录 $fields 是 JSON 格式 =>

 array(8) {
    [0] => array(2) {
        ["name"] => string(26) "checkboxes-label---3499163"  
        ["value"] => string(7) "Options"
    }
    [1] => array(2) {
        ["name"] => string(25) "single-checkbox---3499163"  
        ["value"] => string(34) "Aide à l installation du parcours"
    }
    [2] => array(2) {
        ["name"] => string(31) "single-checkbox---3499163+price"  
        ["value"] => string(3) "500"
    }
    [3] => array(2) {
        ["name"] => string(25) "single-checkbox---3499163"  
        ["value"] => string(27) "Location du système vidéo"
    }
    [4] => array(2) {
        ["name"] => string(31) "single-checkbox---3499163+price"  
        ["value"] => string(2) "10"
    }
    [5] => array(2) {
        ["name"] => string(25) "single-checkbox---3499163"  
        ["value"] => string(4) "test"
    }
    [6] => array(2) {
        ["name"] => string(31) "single-checkbox---3499163+price" 
        ["value"] => string(2) "13"
    }
    [7] => array(2) {
        ["name"] => string(18) "required---3499163"  
        ["value"] => bool(false)
    }
}

这样当我转储 echo var_dump($field) =>

array(2) {
    ["name"] => string(25) "single-checkbox---3499163" 
    ["value"] => string(34) "Aide à l installation du parcours"
}

array(2) {
    ["name"] => string(31) "single-checkbox---3499163+price" 
    ["value"] => string(3) "500"
}

我不确定完成它的最佳方法,我用 count、in_array 和 explode 方法搜索但没有成功

【问题讨论】:

    标签: php conditional-statements


    【解决方案1】:

    没有正则表达式最准确,您只需要检查条件块中的最后 6 个字符是否匹配。

    foreach ($fields as $field) {
        if (substr($field['name'], -6) !== "+price") {
            echo "<input type=\"text\" class=\"optionname\" name=\"{$field['name']}\" value=\"{$field['value']}\"/>";
        } else {
            echo "Prix : <input type=\"text\" class=\"optionprix\" name=\"{$field['name']}+price\" value=\"$optionprix\">";
        }
    }
    

    【讨论】:

      【解决方案2】:

      这仅检查名称是否以“价格”结尾

      if(preg_match('/price$/', $field['name'])) {
          //Prix input
      } else{
          //normal input
      }
      

      【讨论】:

        【解决方案3】:

        您可以使用strpos() 非常简单地做到这一点,如下所示

        <?php
        foreach($fields as $field):
            if ( strpos($field['name'], '+price') === FALSE ) :
        ?>
                <input type="text" class="optionname" name="<?php echo $field['name']; ?>" value="<?php echo $field['value']; ?>"/>
        
        <?php
            else:
        ?>
                Prix : <input type="text" class="optionprix" name="<?php echo $field['name'].'+price'; ?>" value="<?php echo $optionprix; ?>"
        
        <?php
            endif;
        endforeach;
        ?>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-03-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多