PHP迭代到$ _ POST和使用价值的名字 [英] PHP Iterate through $_POST and use values by name

查看:106
本文介绍了PHP迭代到$ _ POST和使用价值的名字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个字段的名称与ITEM1形式,项目2,item13,item43等,每次因为它们在与AJAX的形式填充这些字段是不同的。

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

 的foreach($ _ POST ['itemX'] ['tagsX']为$标签){
    inserttag($标记,X);
  }

其中X = 1,2,13,43等。如何,我可以遍历$ _POST价值观和执行上面只对那些他们的名字开头后面跟着一个的X标识'项目'的价值观?

基于Piontek的回答解决方法:

张贴的数据格式如下:

  [item38] =>阵列([tags38] =>阵列([0] => AAA,[1] => BBB))
[item40] =>阵列([tags40] =>数组([0] => CCC,[1] => DDD))
[ITEM1] =>阵列([tags1] =>阵列([0] => EEE,[1] => ZZZ))

这就是我如何解析和使用它:

 的foreach($ _ POST为$关键=> $值){
    如果(的strstr($键,'项目')){
        $ n = str_replace函数('项目','',$键);
        的foreach($ _ POST ['项目'。$ ID] ['标签'。$ ID]为$标签){
            inserttag($标签的$ id);
        }
    }
}


解决方案

 的foreach($ _ POST为$关键=> $值)
{
    如果(的strstr($键,'项目'))
    {
        $ X = str_replace函数('项目','',$键);
        inserttag(价值$,$ X);
    }
}

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.

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?

Solution based on Piontek's answer:

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

解决方案

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

这篇关于PHP迭代到$ _ POST和使用价值的名字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆