php PHP中的“=>”是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1241819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does "=>" mean in PHP?
提问by Léo Léopold Hertz ??
What does the =>operator mean in the following code?
=>以下代码中的运算符是什么意思?
foreach ($user_list as $user => $pass)
The code is a comment at PHP.net.
The user does not specify the value of $user_list, $useror $pass.
I normally see that =>means equal or greater than.
该代码是 PHP.net 上的注释。用户未指定$user_list、$user或的值$pass。我通常看到这=>意味着等于或大于。
However, I am not sure about its purpose here because it is not assigned. I read the code as
但是,我不确定它在这里的用途,因为它没有被分配。我把代码读为
- process a list of users in integers
- such that the value of each user is equal or greater than password
- 处理整数用户列表
- 使得每个用户的值等于或大于密码
The above does not make sense to me.
以上对我来说没有意义。
回答by hobodave
=>is the separator for associative arrays. In the context of that foreach loop, it assigns the key of the array to $userand the value to $pass.
=>是关联数组的分隔符。在 foreach 循环的上下文中,它将数组的键分配给$user,将值分配给$pass。
Example:
例子:
$user_list = array(
'dave' => 'apassword',
'steve' => 'secr3t'
);
foreach ($user_list as $user => $pass) {
echo "{$user}'s pass is: {$pass}\n";
}
// Prints:
// "dave's pass is: apassword"
// "steve's pass is: secr3t"
Note that this can be used for numerically indexed arrays too.
请注意,这也可用于数字索引数组。
Example:
例子:
$foo = array('car', 'truck', 'van', 'bike', 'rickshaw');
foreach ($foo as $i => $type) {
echo "{$i}: {$type}\n";
}
// prints:
// 0: car
// 1: truck
// 2: van
// 3: bike
// 4: rickshaw
回答by Tyler Carter
It means assign the key to $user and the variable to $pass
这意味着将密钥分配给 $user 并将变量分配给 $pass
When you assign an array, you do it like this
当你分配一个数组时,你这样做
$array = array("key" => "value");
It uses the same symbol for processing arrays in foreach statements. The '=>' links the key and the value.
它使用相同的符号来处理 foreach 语句中的数组。'=>' 链接键和值。
According to the PHP Manual, the '=>' created key/value pairs.
根据PHP 手册,'=>' 创建了键/值对。
Also, Equal or Greater than is the opposite way: '>='. In PHP the greater or less than sign always goes first: '>=', '<='.
此外,等于或大于是相反的方式:'>='。在 PHP 中,大于号或小于号总是在前面:'>='、'<='。
And just as a side note, excluding the second value does not work like you think it would. Instead of only giving you the key, It actually only gives you a value:
作为旁注,排除第二个值并不像您想象的那样工作。而不是只给你钥匙,它实际上只给你一个价值:
$array = array("test" => "foo");
foreach($array as $key => $value)
{
echo $key . " : " . $value; // Echoes "test : foo"
}
foreach($array as $value)
{
echo $value; // Echoes "foo"
}
回答by Pascal MARTIN
Code like "a => b" means, for an associative array (some languages, like Perl, if I remember correctly, call those "hash"), that 'a' is a key, and 'b' a value.
像“a => b”这样的代码意味着,对于关联数组(某些语言,如Perl,如果我没记错的话,称其为“散列”),“a”是键,“b”是值。
You might want to take a look at the documentations of, at least:
您可能想查看至少以下文件的文档:
Here, you are having an array, called $user_list, and you will iterate over it, getting, for each line, the key of the line in $user, and the corresponding value in $pass.
在这里,您有一个名为 的数组,$user_list您将对其进行迭代,为每一行获取 中该行的键$user,以及 中相应的值$pass。
For instance, this code:
例如,这段代码:
$user_list = array(
'user1' => 'password1',
'user2' => 'password2',
);
foreach ($user_list as $user => $pass)
{
var_dump("user = $user and password = $pass");
}
Will get you this output:
会给你这个输出:
string 'user = user1 and password = password1' (length=37)
string 'user = user2 and password = password2' (length=37)
(I'm using var_dumpto generate a nice output, that facilitates debuging; to get a normal output, you'd use echo)
(我var_dump用来生成一个不错的输出,这有利于调试;要获得正常的输出,您可以使用echo)
"Equal or greater" is the other way arround: "greater or equals", which is written, in PHP, like this; ">="
The Same thing for most languages derived from C: C++, JAVA, PHP, ...
“等于或大于”是另一种方式:“大于或等于”,用 PHP 编写,如下所示;">="
对于大多数从 C 派生的语言来说都是一样的:C++、JAVA、PHP、...
As a piece of advice: If you are just starting with PHP, you should definitely spend some time (maybe a couple of hours, maybe even half a day or even a whole day)going through some parts of the manual :-)
It'd help you much!
作为一条建议:如果您刚开始使用 PHP,您绝对应该花一些时间(可能是几个小时,甚至可能是半天甚至一整天)来阅读手册的某些部分:-)
它'对你有很大帮助!
回答by Scharrels
An array in PHP is a map of keys to values:
PHP 中的数组是键到值的映射:
$array = array();
$array["yellow"] = 3;
$array["green"] = 4;
If you want to do something with each key-value-pair in your array, you can use the foreachcontrol structure:
如果你想对数组中的每个键值对做一些事情,你可以使用foreach控制结构:
foreach ($array as $key => $value)
The $array variable is the array you will be using. The $key and $value variables will contain a key-value-pair in every iteration of the foreachloop. In this example, they will first contain "yellow" and 3, then "green" and 4.
$array 变量是您将使用的数组。$key 和 $value 变量将在循环的每次迭代中包含一个键值对foreach。在此示例中,它们将首先包含“黄色”和 3,然后是“绿色”和 4。
You can use an alternative notation if you don't care about the keys:
如果您不关心键,则可以使用替代符号:
foreach ($array as $value)
回答by Meredith L. Patterson
Arrays in PHP are associative arrays (otherwise known as dictionaries or hashes) by default. If you don't explicitly assign a key to a value, the interpreter will silently do that for you. So, the expression you've got up there iterates through $user_list, making the key available as $userand the value available as $passas local variables in the body of the foreach.
默认情况下,PHP 中的数组是关联数组(也称为字典或哈希)。如果你没有明确地为一个值分配一个键,解释器会默默地为你做这件事。所以,你已经通过了那里迭代的表达$user_list,使钥匙可以作为$user和可作为价值$pass作为的身体局部变量foreach。
回答by Mizu
$user_listis an array of data which when looped through can be split into it's name and value.
$user_list是一个数据数组,循环时可以将其拆分为名称和值。
In this case it's name is $userand it's value is $pass.
在这种情况下,它的名字是$user,它的价值是$pass。

