usig的正确方法是什么?和'在PHP和HTML中的子数组中? [英] What is the right way of usig " and ' inside sub-arrays in PHP and HTML?
问题描述
以下哪种方法是在PHP的子数组中使用字符和的正确方法?
$_POST["login['username']"];
#2我在PHP中的原始方式
#2 My original way in PHP
$_POST['login[username]'];
更改所有我的代码后,现在我的代码已损坏
My code is now broken after changing all my variables to arrays.
以下是变量所引用的HTML。
The following is the HTML to which the variables refer.
<p>Username:
<input name="login['username']" type="text" cols="92" />
</p>
<p>Email:
<input name="login['email']" type="text" cols="92" />
</p>
<p>Password:
<input name="login['password']" type="password" cols="92" />
</p>
<input type="submit" value="OK" />
</form>
以下是在HTML中使用数组的正确方法之一。
<input name="login['password']" type="password" cols="92" />
#22
#22
<input name="login[password]" type="password" cols="92" />
我现在正在使用#1和#11方式失败吗?
推荐答案
首先,正确的html格式是#22的格式(不带引号)。
First of all, the correct form in html is what you have for #22 (no quotes).
其次,这样做的全部目的是因为它将转换为数组。发布此表单后,将在$ _POST的内部创建一个名为login的数组。要访问它,请尝试以下操作:
Secondly, the entire point of doing this is because it will convert it into an array. When this form is posted, an array is created INSIDE of $_POST called login. To access it, try this:
echo $_POST['login']['username']; //echos username
echo $_POST['login']['password']; //echos password
以下是嵌套外观的快速概述:
Here's a quick overview of how the nesting looks:
'_POST' =>
array
'login' =>
array
'username' => string 'myusername' (length=10)
'password' => string 'mysecretpassword' (length=16)
尝试执行此操作以了解发生的情况并获得类似上面的输出:
Try doing this to get a good idea of what's going on and get output like above:
echo "<pre>";
var_dump($_POST);
echo "</pre>";
您将能够看到所有嵌套。
You'll be able to see all the nesting.
这篇关于usig的正确方法是什么?和'在PHP和HTML中的子数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!