title: PHP Iterate through $_POST and use values by name
body: <p>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.</p>
  
  <p>When user submits I need to perform the following: </p>
  
  <pre><code>  foreach($_POST['itemX']['tagsX'] as $tag){
      inserttag($tag, X);
    }
  </code></pre>
  
  <p>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? </p>
  
  <p><strong>Solution based on Piontek's answer:</strong></p>
  
  <p>The posted data has the following format:</p>
  
  <pre><code>[item38] =&gt; Array([tags38] =&gt; Array([0] =&gt; aaa,[1] =&gt; bbb))
  [item40] =&gt; Array([tags40] =&gt; Array([0] =&gt; ccc,[1] =&gt; ddd))
  [item1] =&gt;  Array([tags1] =&gt; Array([0] =&gt; eee,[1] =&gt; zzz))
  </code></pre>
  
  <p>And this is how I parse and use it:</p>
  
  <pre><code>foreach($_POST as $key =&gt; $value){
      if (strstr($key, 'item')){
          $id = str_replace('item','',$key); 
          foreach($_POST['item'.$id]['tags'.$id] as $tag){
              inserttag($tag, $id);
          }   
      }
  }
  </code></pre>