(PHP) 如何通过表达式引用 $_POST[] 数组中的元素?

发布于 2024-12-08 12:18:44 字数 616 浏览 2 评论 0原文

我想知道是否设置了变量。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

相对绾红妆 2024-12-15 12:18:44

嗯...不要。请改用数组访问:

 <input type="checkbox" name="deleteFileButton[0]" />
 <input type="checkbox" name="deleteFileButton[1]" />
 <input type="checkbox" name="deleteFileButton[2]" />

如果上面选中了第一项和第三项,那么当您迭代 $_POST['deleteFileButton'] 时,您将获得 0 和 2 的值,而不是 1 的值。

 foreach( $_POST['deleteFileButton'] as $key => $val )
      print "$key was clicked."

如果您例如,您可以将其更改为关联数组(例如,deleteFileButton[myKey]),并且您可以设置输入的值,以便 $val 具有一些有意义的值。


如果您觉得 _ 是您唯一的选择,那么您可以按照其他发帖者的建议使用 issetarray_key_exists,但没有办法提前知道最大输入值而不将其存储在其他地方。坦率地说,它比使用数组效率低且不太优雅。

Um... don't. Use array access instead:

 <input type="checkbox" name="deleteFileButton[0]" />
 <input type="checkbox" name="deleteFileButton[1]" />
 <input type="checkbox" name="deleteFileButton[2]" />

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.

 foreach( $_POST['deleteFileButton'] as $key => $val )
      print "$key was clicked."

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 use isset or array_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.

梦旅人picnic 2024-12-15 12:18:44

将我的答案留在这里,因为它适合该问题,但 @cwallenpoole 的答案即使不是问题,也更适合该问题。


您可以使用 array_key_exists($key, $_POST) 来支付 $ _POST 如果我正确理解你的问题。

而且您还可以访问指定另一个变量作为 $_POST 的键,只要它返回正确的值即可。例如:

$key = 'DeleteFileButton2_x'
$value = $_POST[$key]

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:

$key = 'DeleteFileButton2_x'
$value = $_POST[$key]
别再吹冷风 2024-12-15 12:18:44

PHP 允许您使用任何可转化为整数或字符串的内容来引用数组,因此您绝对可以这样做:

function x() {
   return "a";
}

$x = 'a';

$val = $_POST[x()];
$val = $_POST[$x];
$val = $_POST['a'];
$val = $_POST[chr(ord('a'))];

也允许使用“复合”键:

$_POST["someprefixstring_$x"];  // exactly the same as $_POST['someprefixstring_a'];

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:

function x() {
   return "a";
}

$x = 'a';

$val = $_POST[x()];
$val = $_POST[$x];
$val = $_POST['a'];
$val = $_POST[chr(ord('a'))];

"composite" keys are allowed as well:

$_POST["someprefixstring_$x"];  // exactly the same as $_POST['someprefixstring_a'];
橪书 2024-12-15 12:18:44
<input name="asd[]" value="a">
<input name="asd[]" value="b">
<input name="asd[]" value="c">
<input name="asd[]" value="d">

<?php
foreach($_POST['asd'] as $key => $value)
   echo $_POST['asd'][$key];
?>
<input name="asd[]" value="a">
<input name="asd[]" value="b">
<input name="asd[]" value="c">
<input name="asd[]" value="d">

<?php
foreach($_POST['asd'] as $key => $value)
   echo $_POST['asd'][$key];
?>
养猫人 2024-12-15 12:18:44

首先我想说 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文