Possible Duplicate:
What does $k => $v in foreach($ex as $k=>$v) mean?
I am trying to understand what this means:
foreach($this->domains as $domain=>$users) {
// some code...
}
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
.
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';
}
Outputs:
example.com, user1
fred.com, user2
(In your example, $users
is probably an array of users);
Think of it this way:
foreach($this->domains as $key=>$value) {
It will step through each element in the associative array returned by $this->domains
as a key/value pair. The key will be in $domain
and the value in $users
.
To help you understand, you might put this line in your foreach
loop:
echo $domain, " => ", $users;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With