Get the current array key inside foreach

arrays, foreach, forms, php

Solution

<input type="text" name="attribute[20]"> inputted value = height
<input type="text" name="attribute[27]"> inputted value = width

foreach ($_POST['attribute'] as $attributes){
    echo key($attributes).' '.$attributes.'<br>';
}

Note here that you are looping over the attribute array in post. $attributes is the value for each field (and is therefore not an array.

Instead of using `key()` try:

foreach ($_POST['attribute'] as $attributeKey => $attributes){
    echo $attributeKey.' '.$attributes.'<br>';
}

Problem

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) ``` <input type="text" name="attribute[20]"> inputted value = height <input type="text" name="attribute[27]"> inputted value = width ``` the numbers are generated based on things in the database, so 20 would correlate to "width" 27 would correlate to "height" for example. So once the user enters the values I need those values to go into a database...or in the test, echo out. ``` foreach ($_POST['attribute'] as $attributes){ echo key($attributes).' '.$attributes.'<br>'; } ``` So that should output... ``` 20 height value<br> 27 width value ``` but instead it outputs ``` &nbsp;height value<br> &nbsp;width value ``` What is going on? I have something similar...but slightly different as the defined numbers can have more than one input....which works perfectly. ``` <input type="text" name="option[][20]"> inputted value = option 1 <input type="text" name="option[][20]"> inputted value = option 2 <input type="text" name="option[][27]"> inputted value = option 1 foreach ($_POST['option'] as $options){ echo key($options).' '; foreach ($options as $option){ echo $option.'<br>'; } ``` which outputs perfectly... ``` 20 option 1<br> 20 option 2<br> 27 option 1 ``` 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. EDIT: Var dump as requested 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) { [0]=> string(5) "width" [1]=> string(6) "height" } ["option"]=> array(3) { [0]=> array(1) { [11]=> string(6) "works1" } [1]=> array(1) { [10]=> string(6) "works1" } [2]=> array(1) { [10]=> string(6) "works2" } } ["pLongdescription"]=> string(0) "" ["meta_description"]=> string(0) "" ["meta_keyword"]=> string(0) "" ["meta_title"]=> string(0) "" ["action"]=> string(6) "create" } 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.

Original source