PHP在foreach循环中等于箭头运算符 [英] PHP equals arrow operator in a foreach loop

查看:70
本文介绍了PHP在foreach循环中等于箭头运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
$ k是什么?在foreach中的$ v($ ex是$ k => $ v)是什么意思?

我试图理解这意味着什么:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆