(PHP) 如何通过表达式引用 $_POST[] 数组中的元素?
我想知道是否设置了变量。 PHP 的 isset() 函数(即 isset($myVariable) 通常会为我提供变量的设置状态。但是,我想将变量作为表达式引用。这可能吗?如果可以,如何实现?
更多上下文和细节:我有几个 HTML 表单中的图像提交按钮,每个按钮名为“DeleteFileButtonNNN”,其中 NNN 是一个整数(实际上是一个文件 ID 号)。 NNN=2,表单已提交并且 $_POST['DeleteFileButton2_x'] 被设置(而 $_POST['DeleteFileButton1_x' 和 $_POST['DeleteFileButton3_x'] 将不会被设置)。 _POST 数组以便发现单击了哪个按钮,
我可以通过 while 或 foreach 循环轻松创建循环本身的边界,因为所有可能的文件。 ID(即 NNN 值)存储在 MySQL 表中,我需要帮助如何指定各种 DeleteFileButtonNNN 元素作为 $_POST 数组中的数组元素索引,因为引用需要是一个表达式(例如,字符串连接,如 'DeleteFileButton'.$anInteger.'_x')。但我不相信 PHP 允许我将 $_POST 数组引用为 $_POST[$someVariable]。
有人可以帮我吗?先感谢您。
I want to discover whether or not a variable is set. PHP's isset() function (i.e. isset($myVariable)would ordinarily provide me with the variable's set status. However, I want to reference the variable as an expression. Is that possible? and if so, how?
A little more context and detail: I have several image submit buttons in an HTML form, each named 'DeleteFileButtonNNN' where NNN is an integer (actually, a file ID number). When the user clicks, say, the button associated with NNN=2, the form is submitted and $_POST['DeleteFileButton2_x'] gets set (whereas $_POST['DeleteFileButton1_x' and $_POST['DeleteFileButton3_x'] will not be set). I want to be able to loop through the $_POST array in order to discover which button was clicked.
I can easily create the boundaries of the loop itself via a while or foreach loop because all possible file IDs (i.e. NNN values) are stored in a MySQL table. My need for help is in how to specify the various DeleteFileButtonNNN elements as array element indices in the $_POST array since the reference would need to be an expression (e.g. a string concatenation such as 'DeleteFileButton'.$anInteger.'_x'). Yet I don't believe PHP allows me to reference the $_POST array as $_POST[$someVariable].
Could someone me help please? Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
嗯...不要。请改用数组访问:
如果上面选中了第一项和第三项,那么当您迭代
$_POST['deleteFileButton']
时,您将获得 0 和 2 的值,而不是 1 的值。如果您例如,您可以将其更改为关联数组(例如,
deleteFileButton[myKey]
),并且您可以设置输入的值,以便 $val 具有一些有意义的值。如果您觉得
_
是您唯一的选择,那么您可以按照其他发帖者的建议使用isset
或array_key_exists
,但没有办法提前知道最大输入值而不将其存储在其他地方。坦率地说,它比使用数组效率低且不太优雅。Um... don't. Use array access instead:
If the first and third items are checked above, then when you iterate through
$_POST['deleteFileButton']
you'll get values for 0 and 2 and not for 1.If you like, you can change it to associative array (eg.
deleteFileButton[myKey]
) and you can set the value of the input so that $val has some meaningful value.If you feel that
<key>_<index>
is your only option then you can useisset
orarray_key_exists
as other posters have suggested, but there will not be a way of knowing the maximum input value ahead of time without storing it somewhere else. Frankly, it is a less efficient and less elegant than using an array.将我的答案留在这里,因为它适合该问题,但 @cwallenpoole 的答案即使不是问题,也更适合该问题。
您可以使用 array_key_exists($key, $_POST) 来支付 $ _POST 如果我正确理解你的问题。
而且您还可以访问指定另一个变量作为 $_POST 的键,只要它返回正确的值即可。例如:
Leaving my answer here as it is appropriate to the question but @cwallenpoole's answer is better for the problem if not the question.
You could use array_key_exists($key, $_POST) for $_POST if I understand your question correctly.
And also you can access a specify another variable as the key to $_POST as long as it returns the correct value. e.g:
PHP 允许您使用任何可转化为整数或字符串的内容来引用数组,因此您绝对可以这样做:
也允许使用“复合”键:
PHP allows you to use anything that devolves down to an integer or a string to reference an array, so you can most definitely do:
"composite" keys are allowed as well:
首先我想说 php 允许您将 $_POST 数组引用为 $_POST[$someVariable]。一个简单的实现是使用隐藏输入字段并在提交表单之前使用 javascript 将隐藏字段的值替换为图像 id,并在您的 php 页面中获取此隐藏输入作为将执行操作的图像的 id这将节省许多 if-else 结构
first i would say php allow you to reference the $_POST array as $_POST[$someVariable]. an easy implementation would be to use a hidden input field and using javascript before submitting the form to replace the value of hidden field with the image id and in your php page get this hidden input as id of the image on which the operation will be performed this will saves many of the if-else constructs