PHP Iterate through $_POST and use values by name

前端 未结 4 1405
醉酒成梦
醉酒成梦 2021-02-05 05:14

I have a form that contains a number of fields with names item1, item2, item13, item43 etc, each time those fields are different because they are populated in the form with AJAX

相关标签:
4条回答
  • 2021-02-05 05:53
    foreach($_POST as $key => $value)
    {
        if (strstr($key, 'item'))
        {
            $x = str_replace('item','',$key);
            inserttag($value, $x);
        }
    }
    
    0 讨论(0)
  • 2021-02-05 05:54

    You can loop through $_POST with foreach like this:

    foreach ($_POST as $key => $value) { ... }
    

    And within the loop you can evaluate whether each key found by the loop matches your criteria. Something like this:

    foreach ($_POST as $key => $value){
       if (substr($key, 0, 4) == "item") {
          $identifier = substr($key, 4);
          if (isset($value['tag' . $identifier])) { inserttag('tag', $identifier); }
       }
    }
    

    I'm not 100% sure what is actually real and what is just a placeholder in your question though. Maybe I took something for solid fact that actually isn't. You might need to explain your wishes in more detail. ;)

    0 讨论(0)
  • 2021-02-05 05:59

    Loop through $_POST and see if the key contains 'item'.

    foreach($_POST as $key=>$value){
        if(preg_match('/item(\d*)/', $key, $match) === 1){
            inserttag($value, $match[1]);
        }
    }
    
    0 讨论(0)
  • 2021-02-05 06:05

    Try:

    foreach($_POST as $key=>$value){
        inserttag($key, $value);
    }
    

    $key will be the name of the element and $value will be the value.

    0 讨论(0)
提交回复
热议问题