Parse the first level keys of $_POST, then use the numeric suffix while looping
Asked Answered
B

6

27

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?

The posted data has the following format:

$_POST = [
    'item38' => ['tags38' => ['aaa', 'bbb']],
    'item40' => ['tags40' => ['ccc', 'ddd']],
    'item1' => ['tags1' => ['eee', 'zzz']],
];
Binate answered 9/1, 2013 at 20:10 Comment(0)
G
38
foreach($_POST as $key => $value)
{
    if (strstr($key, 'item'))
    {
        $x = str_replace('item','',$key);
        inserttag($value, $x);
    }
}
Gambado answered 9/1, 2013 at 20:16 Comment(1)
The PHP documentation has an explicit note that says not to use strstr() to check the existence of a substring for performance reasons.Kalikalian
M
5

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

Mohammedmohammedan answered 9/1, 2013 at 20:17 Comment(0)
A
4

Try:

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

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

Allpurpose answered 9/1, 2013 at 20:11 Comment(0)
A
1

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]);
    }
}
Atlantis answered 9/1, 2013 at 20:15 Comment(4)
Wouldn't you have to iterate through $value looking for keys that match tagsX then pass those values to inserttagMoralez
I was just thinking this @Musa!Coronado
@Musa: Reading the question again, yeah, sounds like I would. I'll leave that as a challenge for the OP ^^Atlantis
Please check my question, I've updated it with the solution. Thanks everyone for your time and effort.Binate
K
1

Use sscanf() to validate and extract the identifying integer with one function call.

Code: (Demo)

foreach ($_POST as $itemId => $set) {
    if (sscanf($itemId, 'item%d', $id)) {
        foreach ($set["tags$id"] as $v) {
            inserttag($v, $id);
        }
    }
}
Kalikalian answered 5/12, 2023 at 7:40 Comment(0)
G
0

If your item numbers are sequential, it's possible to use FOR to this intent. Concatenate the number ($i) with the item name on the index, like that:

for ($i=0; $i <= $numbeOfItems; $i++) {
 inserttag($_POST['item' . $i]['tags' . $i]), $i;
}
Gauthier answered 25/2, 2021 at 13:48 Comment(1)
The asked question does not indicate that the input array is indexed.Kalikalian

© 2022 - 2024 — McMap. All rights reserved.