PHP在foreach循环中等于箭头运算符 [英] PHP equals arrow operator in a foreach loop
问题描述
我试图理解这意味着什么:
I am trying to understand what this means:
foreach($this->domains as $domain=>$users) {
// some code...
}
我了解 $ this-&>; domains
是 foreach
将会建立索引的数组.但是 as $ domain => $ users
是什么意思?我只看到数组中使用 =>
运算符来设置(键,值)对.该类有一个名为 $ domain
的成员,但我认为可以通过 $ this-> domain
进行访问.
I understand $this->domains
is an array that foreach
will index over. But what does as $domain=>$users
mean? I have only seen the =>
operator used in an array to set (key, value) pairs. The class has a member called $domain
, but I assume that would be accessed as $this->domain
.
推荐答案
=>
运算符指定关联.因此,假设 $ this-> domains
是一个数组,则 $ domain
是键,而 $ users
是值.>
The =>
operator specifies an association. So assuming $this->domains
is an array, $domain
will be the key, and $users
will be the value.
<?php
$domains['example.com'] = 'user1';
$domains['fred.com'] = 'user2';
foreach ($domains as $domain => $user) {
echo '$domain, $user\n';
}
输出:
example.com, user1
fred.com, user2
(在您的示例中, $ users
可能是用户数组);
(In your example, $users
is probably an array of users);
这篇关于PHP在foreach循环中等于箭头运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!