title: Get the current array key inside foreach
body: <p>Ok so, I am building something for my employer for them to input products, they have very specific requirements. I have a form with dynamically generated fields like so...
  (obviously not the exact code to follow but the examples are identical conceptually)</p>
  
  <pre><code>&lt;input type="text" name="attribute[20]"&gt; inputted value = height
  &lt;input type="text" name="attribute[27]"&gt; inputted value = width
  </code></pre>
  
  <p>the numbers are generated based on things in the database, so 20 would correlate to "width" 27 would correlate to "height" for example.</p>
  
  <p>So once the user enters the values I need those values to go into a database...or in the test, echo out.</p>
  
  <pre><code>foreach ($_POST['attribute'] as $attributes){
  echo key($attributes).' '.$attributes.'&lt;br&gt;';
  }
  </code></pre>
  
  <p>So that should output...</p>
  
  <pre><code>20 height value&lt;br&gt;
  27 width value
  </code></pre>
  
  <p>but instead it outputs</p>
  
  <pre><code>&amp;nbsp;height value&lt;br&gt;
  &amp;nbsp;width value
  </code></pre>
  
  <p>What is going on? I have something similar...but slightly different as the defined numbers can have more than one input....which works perfectly.</p>
  
  <pre><code>&lt;input type="text" name="option[][20]"&gt; inputted value = option 1
  &lt;input type="text" name="option[][20]"&gt; inputted value = option 2
  &lt;input type="text" name="option[][27]"&gt; inputted value = option 1
  
  foreach ($_POST['option'] as $options){
  echo key($options).' ';
  foreach ($options as $option){
  echo $option.'&lt;br&gt;';
  }
  </code></pre>
  
  <p>which outputs perfectly...</p>
  
  <pre><code>20 option 1&lt;br&gt;
  20 option 2&lt;br&gt;
  27 option 1
  </code></pre>
  
  <p>I don't understand why the more complex one works and the simpler one doesn't, am I missing something obvious? I am aware I have a somewhat unorthodox method of coding in comparison to some, but it is what it is lol. Any help would be greatly appreciated.</p>
  
  <p>EDIT: Var dump as requested</p>
  
  <p>array(22) { ["pID"]=> string(12) "test product" ["pPrice"]=> string(0) "" ["pName"]=> string(0) "" ["pRRP"]=> string(0) "" ["pPostSize"]=> string(0) "" ["pOurPrice"]=> string(0) "" ["pEstDelivery"]=> string(0) "" ["pWeight"]=> string(0) "" ["pEAN"]=> string(0) "" ["pOrder"]=> string(0) "" ["pStock"]=> string(0) "" ["pManufacturer"]=> string(0) "" ["pType"]=> string(13) "Shower Valves" ["pRange"]=> string(0) "" ["cat"]=> array(2) { [0]=> string(2) "72" [1]=> string(2) "23" } ["attribute"]=> array(2) { [<strong><em>0</em></strong>]=> string(5) "<strong><em>width</em></strong>" [1]=> string(6) "height" } ["option"]=> array(3) { [0]=> array(1) { [<strong>11</strong>]=> string(6) "<strong>works1</strong>" } [1]=> array(1) { [<strong>10</strong>]=> string(6) "<strong>works1</strong>" } [2]=> array(1) { [<strong>10</strong>]=> string(6) "<strong>works2</strong>" } } ["pLongdescription"]=> string(0) "" ["meta_description"]=> string(0) "" ["meta_keyword"]=> string(0) "" ["meta_title"]=> string(0) "" ["action"]=> string(6) "create" }</p>
  
  <p>the bold parts, are the parts that successfully come out in my second example. but the bold italic as you can see, returns 0 instead of the 20 that is actually in the form name value.</p>