获取foreach中的当前数组键 [英] Get the current array key inside foreach
问题描述
(显然不是确切的代码,但在概念上是相同的例子)
< input type =textname =attribute [20]>输入值=高度
<输入类型=文本名称=属性[27]>输入值=宽度
这些数字是基于数据库中的东西生成的,因此20会与宽度27将与高度相关,例如。
所以一旦用户输入值我需要这些值进入数据库...或在测试,$ echo $。
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ foreach属性)。''。$ attributes。'< br>';
$ b
所以应该输出...
<20>高度值
27宽度值
但是输出
身高值
宽度值
发生了什么事?我有类似的...但稍有不同,因为定义的数字可以有多个输入....这完美的作品。
< input type =textname =option [] [20]>输入值=选项1
<输入类型=文本名称=选项[] [20]>输入值=选项2
<输入类型=文本名称=选项[] [27]>输入值=选项1
foreach($ _POST ['option'] as $ options){
echo key($ options)。'';
foreach($ options as $ option){
echo $ option。'< br>';
$ / code>
完美输出...
20选项1
20选项2
27选项1
我不明白为什么更复杂的工作,更简单的不工作,我是否错过了一些明显的东西?我知道我有一些比较有些非正统的编码方法,但这是它是什么大声笑。任何帮助将不胜感激。
编辑:Var转储请求
array(22){[ (0)[pRRP] => string(0)p] => string(12)test product[pPrice] => string pPostSize=> string(0)[pOurPrice] => string(0)[pEstDelivery] => string(0)[pWeight] => string( 0)[pEAN] => string(0)[pOrder] => string(0)[pStock] => string(0)[pManufacturer] => (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) strong> works2 }} [pLongdescription] => st ring(0)[meta_description] => string(0)[meta_keyword] => string(0)[meta_title] => string(0)[action] => string(6)create}
粗体部分是我第二个例子中成功出现的部分。但你可以看到,粗体斜体,返回0,而不是实际上在表单名称值20。
< input type =textname =attribute [20]>输入值=高度
<输入类型=文本名称=属性[27]>输入值=宽度
foreach($ _POST ['attribute'] as $ attributes){
echo key($ attributes)。''。$ attributes。'< br>';
$ b 请注意,您正在循环post中的属性数组。 $ attributes是每个字段的值(因此不是数组)。
而不是使用 key()
试试:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ attributeKey。'。$ attributes。'< br>';
}
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
27 width value
but instead it outputs
height value
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
20 option 2
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.
<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>';
}
这篇关于获取foreach中的当前数组键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!