2018-06-28 11:14
浏览 41


we have a form in which input added dynamically . In form submit page we will get the following result


etc etc 


so here we need to get the last key number , that is wind_n , here n=25

here the last input is ['wind_25'] that's why n=25

Please help .

图片转代码服务由CSDN问答提供 功能建议

我们有一个动态添加输入的表单。 在表单提交页面中,我们将得到以下结果

  print_r($ _ POST)
 ['wind_1'] = hk 
 ['wind_2'] = pop \  netc等
 ['wind_25'] =另一个

所以这里我们需要得到最后一个密钥号,即 wind_n ,这里 n = 25

这里最后一个输入是['wind_25'],这就是为什么n = 25


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dqc18251
    dqc18251 2018-06-28 12:40

    Using regex seems unnecessary here unless you need to perform fullstring validation.

    It seems that you are only checking the static leading characters, so strpos() is the most efficient call.

    I am saving each found key instead of using a counter.

    When the loop finishes, I extract the integer from the last key.

    Code: (Demo)

    $_POST = [
        'wind_1' => 'hk',
        'hamburger_66' => 'foo',
        'wind_2' => 'pop',
        'wind_25' => 'another'
    foreach ($_POST as $k => $v) {
        if (strpos($k, 'wind_') === 0) {  // simple validatation
            $key = $k;  // overwrite previous qualifying key
    echo filter_var($key, FILTER_SANITIZE_NUMBER_INT);  // isolate the number
    // or you could use str_replace('wind_', '', $key);

    Or if you want to get a bit funky...

    echo max(preg_replace('~^(?:wind_|.*)~', '', array_keys($_POST)));

    This replaces all of the leading wind_ substrings OR the whole string, then plucks the highest value.


    P.S. When you are anyone else ascends to PHP7.3 or higher, there is a wonderful function released (array_key_last())to access the last key of an array. (I'm assuming the keys are reliably structured/sorted.)

    Code: (Demo)

    $_POST = [
        'wind_1' => 'hk',
        'wind_2' => 'pop',
        'wind_25' => 'another'
    echo substr(array_key_last($_POST), 5);
    // output: 25

    After all of the above workarounds, I think the best advice would be to change the way you are coding your form fields. If you change the name attribute from wind_# to wind[#], you will create a wind subarray within $_POST and then you can access the number values without dissecting a string. (Demo)

    echo array_key_last($_POST['wind']);

    or (sub PHP7.3)

    echo key($_POST['wind']);
    点赞 评论
  • doupafu6980
    doupafu6980 2018-06-28 11:17
      foreach($_POST as $key=>$value)
      {  if(preg_match('/^wind/',$key))
        { $i=$i+1; }  }
     echo $i;

    here the foreach statement accesses all the key-value pair in $_POST using $key and $value. preg_match() checks for all the keys that start with wind and for every such key the variable $i increments itself. after all the keys are checked, echo $i prints the value of $i and hence the answer.

    点赞 评论
  • dpmkif3097
    dpmkif3097 2018-06-28 11:27
    foreach($_POST as $key => $value) {
       if(strpos($key,"wind_") == 0) $i++;
    echo $i;
    点赞 评论
  • douyigua5381
    douyigua5381 2018-06-28 11:28

    so matching wind with key of $_POST item if it returns true then take further the count and voila it will give you the very last wind_n index. and also this code will give you the advantage of having some other POST variable in array.

    $_POST = array('wind_1' => 'r','wind_2' => 'r','wind_3' => 'r','wind_4' => 'r');
     $i = 0;
     foreach($_POST as $name => $var){
       if(strpos("wind_",$name) === 0)
    echo $i;
    点赞 评论