title: PHP - Loop through POST values N times
body: <p>I'm working with an API that sends a request to my server with some POST values, including this one:</p>
  
  <pre><code>'NumMedia' =&gt; '1'
  </code></pre>
  
  <p>The value for NumMedia determines how many additional parameters will be received. For example if NumMedia = 1 I will get 2 additional parameters like this:</p>
  
  <pre><code>'MediaContentType0' =&gt; 'image/jpeg'
  'MediaUrl0' =&gt; 'https://api.server.com/2010-04-01/Accounts/xxxxxxxx',
  </code></pre>
  
  <p>If NumMedia is greater than 1 I'll get an additional set of parameters like this:</p>
  
  <pre><code>'MediaContentType0' =&gt; 'image/jpeg'
  'MediaUrl0' =&gt; 'https://api.server.com/2010-04-01/Accounts/xxxxxxxx'
  'MediaContentType1' =&gt; 'image/jpeg'
  'MediaUrl1' =&gt; 'https://api.server.com/2010-04-01/Accounts/zzzzzzzz'
  </code></pre>
  
  <p>and so on, where the first parameters end with 0 and increment by 1 for every additional returned set of parameters.</p>
  
  <p>What I would like to do is, based on the number of parameters indicated by the value of the NumMedia parameter, loop through each of these and set them into variables and then write out some database records. Something along the lines of:</p>
  
  <pre><code>if (isset($_POST['NumMedia']) &amp;&amp; $_POST['NumMedia'] !== '') {
      $totalParams = $_POST['NumMedia'];
      $counter = 0;
  
      // set a variable for the first set of parameters
      $mediaContentType = $_POST['NumMediaXXX'] // XXX = $counter
      $mediaURL = $_POST['MediaUrlXXX'] // XXX = $counter
  
      // save these variables to database record
      // increment counter to get next set of parameters
      // exit when $counter = $totalParams
  
      $counter ++; 
  }
  </code></pre>
  
  <p>I can conceptually see what I need to do just not sure how to get the loop going and to reference a dynamic POST parameter and exit the loop when I hit the number of expected parameters?</p>