4

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.

4

3 回答 3

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

请注意,您正在循环 post 中的属性数组。$attributes 是每个字段的值(因此不是数组。

而不是使用key()尝试:

foreach ($_POST['attribute'] as $attributeKey => $attributes){
    echo $attributeKey.' '.$attributes.'<br>';
}
于 2012-12-13T00:20:32.960 回答
2

好的,我已经修复了它,在你们的帮助下,我意识到我有点愚蠢,只编辑了表单的静态部分以合并动态键,而不是覆盖它的 ajax 生成部分。

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

完美运行。谢谢提醒伙计。

于 2012-12-13T01:28:39.987 回答
2

根据key() 的 PHP 文档

key()函数只返回当前由内部指针指向的数组元素的键。它不会以任何方式移动指针。如果内部指针指向元素列表末尾之外或数组为空,则key()返回NULL

文档(和示例)显示您需要提供实际数组作为参数,您在其中使用该值。

所以使用这样的东西:

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

即使您注意到您知道您有“与某些方法相比有些非正统的编码方法”,但foreach以这种方式使用 -loop 会更好:

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

因为该key()方法对我来说似乎有点“狡猾”(依赖于内部指针)。

查看foreach 文档以获取有关此用途的更多信息。

于 2012-12-13T00:22:15.880 回答