是什么让这两个阵列添加不同? [英] What Makes These Two Array Adds Different?
问题描述
我用这把用户的输入,并清理code。我试图解决这个问题了,然后code停止工作。
本作品:
$ BindVar [] =阵列();
$ BindVar [] = $电子邮件;
$ BindVar [] = $通过;
然而,这并不:
$ BindVar [] =阵列($电子邮件,$通行证);
下面是code当我改变该行打破:
的foreach($ BindVars为$值){
$输入[] ='。 $ mysqli-> real_escape_string($值)。 '; //清理输入
}
它给出了这个错误:
警告:mysqli的:: real_escape_string()预计参数1是字符串,在
定的数组
块引用>解决方案了解您的错误信息,我想这大概需要阵列的一些交代。
如果您使用了括号
$ bindVars []
,你不必说了,这是一个数组通过调用阵列()
功能。PHP理解括号
[]
作为数组声明。您声明括号
[]
和的阵列()
函数$ BindVars [] =阵列($电子邮件,$通行证);// 与...一样:
$ BindVars =阵列(
阵列($电子邮件,$通行证);
);创建一个新的磁盘阵列的在的包含的元素
$ bindVars
$电子邮件
&安培;$通
。因此,
$ bindVars
是因为括号[]
已经数组。这给了我们一个多维数组在您的code,这是一个 2维数组,从而重新presents表。这是最经常使用的多维阵列。
A 三维数组的如
$ bindVars [] [] []
(3对括号)再presents立方体或模具。A 4 Dimenisional阵列的
$ bindVars [] [] [] []
(4对安装支架的)再用另一个立方体尺寸在它两侧。的 ---随着每一个进一步尺寸它变得更加令人难以置信--- 的回到 2维数组
在 1。尺寸是行或记录
在 2。尺寸与列或字段。
// 1.尺寸:1的行
回声$ bindVars [0]; //输出:阵列()这是你从哪里来的mysqli错误!
警告:mysqli的:: real_escape_string()预计参数1是
字符串数组给定...
块引用>/ * --- 1.记录/行--- * /
// 2.尺寸:1的行,列1。
回声$ bindVars [0] [0]; //输出:$电子邮件的价值
// 2.尺寸:1的行,列2。
回声$ bindVars [0] [1]; //输出:$通价值 / * --- 2.记录/行--- * /
// 1列
回声$ bindVars [1] [0]; //输出:$电子邮件的价值
// 2列
回声$ bindVars [1] [1]; //输出:$通价值// 等等修改
$ BindVar [] =阵列($电子邮件,$通行证);
到
$ BindVar =阵列($电子邮件,$通行证);
I'm using this to take user input and was cleaning up code. I tried to fix this up and then the code stopped working.
This works:
$BindVar[] = array(); $BindVar[] = $Email; $BindVar[] = $pass;
This however does not:
$BindVar[] = array($Email,$pass);
Here is the code that breaks when I change that line:
foreach ($BindVars as $value) { $input[] = "'" . $mysqli->real_escape_string($value) . "'"; // cleaning the input }
It gives this error:
Warning: mysqli::real_escape_string() expects parameter 1 to be string, array given in
解决方案To understand your error message, I think it needs some explaination about Arrays.
If you use brackets like
$bindVars[]
, you don't need to say, that it's an array by calling thearray()
function.PHP understands the brackets
[]
as array declaration.
Your declarations with brackets
[]
and thearray()
function$BindVars[] = array($Email,$pass); // same as: $BindVars = array( array($Email, $pass); );
creates a new Array inside
$bindVars
containing the elements$pass
.So,
$bindVars
is already an Array because of the brackets[]
. That gives us a Multi-dimensional Array.In your code, it's a 2-Dimensional Array, which represents a table. It's the most frequently used Multi-Dimensional Array.
A 3-Dimensional Array like
$bindVars[][][]
(3 pairs of brackets) represents a cube or a die.A 4-Dimenisional Array
$bindVars[][][][]
(4 pairs of brackets) is then a cube with another dimension on each side of it. --- With every further Dimension it gets more mind-boggling ---Back to the 2-Dimensional Array:
The 1. Dimension is the row or record
The 2. Dimension is the column or field.
// 1. Dimension: 1. row echo $bindVars[0]; // Output: Array() Here is your mysqli error coming from!
Warning: mysqli::real_escape_string() expects parameter 1 to be string, array given ...
/* --- 1. Record/Row --- */ // 2. Dimension: 1. row, 1. column echo $bindVars[0][0]; // Output: value of $Email // 2. Dimension: 1. row, 2. column echo $bindVars[0][1]; // Output: value of $pass /* --- 2. Record/Row --- */ // 1. column echo $bindVars[1][0]; // Output: value of $Email // 2. column echo $bindVars[1][1]; // Output: value of $pass // And so on
Change
$BindVar[] = array($Email,$pass);
to
$BindVar = array($Email,$pass);
这篇关于是什么让这两个阵列添加不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!