Get the current array key inside foreach

后端 未结 3 1811
悲&欢浪女
悲&欢浪女 2021-02-10 23:01

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... (obvi

3条回答
  •  被撕碎了的回忆
    2021-02-10 23:25

     inputted value = height
     inputted value = width
    
    foreach ($_POST['attribute'] as $attributes){
        echo key($attributes).' '.$attributes.'
    '; }

    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.'
    '; }

提交回复
热议问题