PHP 遍历 $_POST 并按名称使用值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14245588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:56:24  来源:igfitidea点击:

PHP Iterate through $_POST and use values by name

phparrays

提问by bikey77

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.

我有一个表单,其中包含许多名称为 item1、item2、item13、item43 等的字段,每次这些字段都不同,因为它们是用 AJAX 填充到表单中的。

When user submits I need to perform the following:

当用户提交时,我需要执行以下操作:

  foreach($_POST['itemX']['tagsX'] as $tag){
    inserttag($tag, X);
  }

where X = 1,2,13,43 etc. How can I iterate the $_POST values and perform the above only for the values of those that their name begins with 'item' followed by an the X identifier?

其中 X = 1,2,13,43 等。如何迭代 $_POST 值并仅对名称以“item”开头且后跟 X 标识符的值执行上述操作?

Solution based on Piontek's answer:

基于 Piontek 的回答的解决方案:

The posted data has the following format:

发布的数据具有以下格式:

[item38] => Array([tags38] => Array([0] => aaa,[1] => bbb))
[item40] => Array([tags40] => Array([0] => ccc,[1] => ddd))
[item1] =>  Array([tags1] => Array([0] => eee,[1] => zzz))

And this is how I parse and use it:

这就是我解析和使用它的方式:

foreach($_POST as $key => $value){
    if (strstr($key, 'item')){
        $id = str_replace('item','',$key); 
        foreach($_POST['item'.$id]['tags'.$id] as $tag){
            inserttag($tag, $id);
        }   
    }
}

回答by Kyle

foreach($_POST as $key => $value)
{
    if (strstr($key, 'item'))
    {
        $x = str_replace('item','',$key);
        inserttag($value, $x);
    }
}

回答by Till Helge

You can loop through $_POSTwith foreachlike this:

您可以通过循环$_POST使用foreach这样的:

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. ;)

不过,我不能 100% 确定什么是真实的,什么只是您问题中的占位符。也许我采取了一些事实,但实际上并非如此。您可能需要更详细地解释您的愿望。;)

回答by SeanWM

Try:

尝试:

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

$keywill be the name of the element and $valuewill be the value.

$key将是元素的名称,$value并将是值。

回答by Rocket Hazmat

Loop through $_POSTand see if the key contains 'item'.

循环$_POST并查看密钥是否包含'item'.

foreach($_POST as $key=>$value){
    if(preg_match('/item(\d*)/', $key, $match) === 1){
        inserttag($value, $match[1]);
    }
}