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
foreach($_POST as $key => $value)
{
if (strstr($key, 'item'))
{
$x = str_replace('item','',$key);
inserttag($value, $x);
}
}
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. ;)
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]);
}
}
Try:
foreach($_POST as $key=>$value){
inserttag($key, $value);
}
$key
will be the name of the element and $value
will be the value.