English 中文(简体)
(PHP) How to reference an elements in$_POST[] 表达方式的阵容?
原标题:(PHP) How to reference an element in $_POST[] array by way of an expression?
  • 时间:2011-10-05 18:56:12
  •  标签:
  • php
  • isset

我想发现,是否设定了变量。 PHP s isset(a)功能(即: isset($myVariable)通常可以向我提供变数的固定地位。 然而,我想将变量作为表述。 这是可能的吗? 如果是,如何?

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.

我可以很容易地通过一旁或旁边的 lo子来划定 lo体的界限,因为所有可能的卷宗(即NNN值)都储存在MySQL的桌上。 我需要帮助的是,具体指明。 删除“FileButtonNNN”各部分,作为“POST”阵列中阵列的阵列要素指数,因为这一提法需要表述(例如,一个扼杀性组,如删除FileButton .$anInteger._x)。 然而,我不相信PHP允许我把“POST”阵列作为“POST[可兑换美元]。

是否有人? 事先感谢你。

最佳回答

PHP允许你使用任何vol到愤怒或指向阵列的东西,因此你可以肯定地做:

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 ];
问题回答

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]" />

如果上面检查了第一和第三件物品,那么,如果通过<代码>复制_POST[删除FileButton],你就会获得0和2而不是1的数值。

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

如果你愿意的话,你可以将其改为联系阵列(例如:deleteFileButton[myKey]),并且你可以确定投入的价值,使阀值具有一定的意义。


如果你认为<代码><key>_<index> 是你的唯一选择,那么你可以像其他海报所建议的那样使用<代码>isset或-key_exists,但将无法事先了解最大投入价值,而不必在其他地方储存。 坦率地说,与使用阵列相比,它效率较低,代表性较低。

我在这里回答这个问题是适当的,但“@ckten Collectivee”回答如果不是问题,会更好。


如果我正确理解你的提问,你可以使用

并且,只要收回正确价值,你还可以获得另一个具体变量,作为美元的关键。 e.g:

$key =  DeleteFileButton2_x 
$value = $_POST[$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];
?>

首先,我要说,php允许你将“POST”阵列作为_POST[一些可变的]美元。 易于执行的做法是,在提交表格以取代隐蔽领域的价值之前,使用javascript,然后以图像id取代隐蔽领域的价值,在你的实验室网页上,这种隐蔽的投入会掩盖其形象,从而节省许多假装建筑。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签