W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
集合通過迭代(Iterating)foreach。
<?hh
namespace Hack\UserDocumentation\Collections\Iterating\Examples\FEach;
function run(): void {
$vec = Vector {'A', 'B', 'C'};
$map = Map {'A' => 1, 'B' => 2};
$set = Set {800, 900, 1000};
$pair = Pair {'A', 'B'};
foreach ($vec as $val) {
var_dump($val);
}
foreach ($map as $key => $val) {
var_dump($key);
var_dump($val);
}
foreach ($set as $val) {
var_dump($val);
}
foreach ($pair as $key => $val) {
var_dump($key);
var_dump($val);
}
}
run();
Output
string(1) "A"
string(1) "B"
string(1) "C"
string(1) "A"
int(1)
string(1) "B"
int(2)
int(800)
int(900)
int(1000)
int(0)
string(1) "A"
int(1)
string(1) "B"
當(dāng)使用它foreach來迭代集合時,有幾點值得注意的注意事項。
不允許從集合添加或刪除項目,如果嘗試,將導(dǎo)致一個InvalidOperationException。
<?hh
namespace Hack\UserDocumentation\Collections\Iterating\Examples\Modifying;
function run(): void {
$vec = Vector {'A', 'B', 'C'};
try {
foreach ($vec as $val) {
if ($val === 'B') {
// This will actually be allowed, but the next time through the loop
// the InvalidOperationException will be thrown.
$vec[] = 'D';
}
var_dump($vec);
var_dump($val);
}
} catch (\InvalidOperationException $ex) {
var_dump($ex->getMessage());
}
$set = Set {100, 200, 300};
try {
foreach ($set as $val) {
if ($val === 300) {
// This will actually be allowed, and even though this is the last
// element in the set, we actually do the check in the foreach again so
// the InvalidOperationException will be thrown.
$set->remove(300);
}
var_dump($set);
var_dump($val);
}
} catch (\InvalidOperationException $ex) {
var_dump($ex->getMessage());
}
}
run();
Output
object(HH\Vector)#1 (3) {
[0]=>
string(1) "A"
[1]=>
string(1) "B"
[2]=>
string(1) "C"
}
string(1) "A"
object(HH\Vector)#1 (4) {
[0]=>
string(1) "A"
[1]=>
string(1) "B"
[2]=>
string(1) "C"
[3]=>
string(1) "D"
}
string(1) "B"
string(40) "Collection was modified during iteration"
object(HH\Set)#3 (3) {
int(100)
int(200)
int(300)
}
int(100)
object(HH\Set)#3 (3) {
int(100)
int(200)
int(300)
}
int(200)
object(HH\Set)#3 (2) {
int(100)
int(200)
}
int(300)
string(40) "Collection was modified during iteration"
請注意,在該示例中,實際的添加或刪除將生效,但是當(dāng)我們回到循環(huán)迭代檢查時將拋出異常。
您不能通過foreach引用作為集合。
foreach ($vec as &$val)
這將導(dǎo)致致命的錯誤,甚至不是例外。
有一種模擬這種行為的方法。
<?hh
namespace Hack\UserDocumentation\Collections\Iterating\Examples\Reference;
function run(): void {
$arr = array ('A', 'B', 'C');
// if $arr was a Vector instead, we would get a
// Fatal error: Collection elements cannot be taken by reference
var_dump($arr);
foreach ($arr as &$val) {
var_dump($val);
if ($val === 'B') {
$val = 'D';
}
var_dump($arr);
}
// How to mimic the above
$vec = Vector {'A', 'B', 'C'};
var_dump($vec);
foreach ($vec as $key => $val) {
var_dump($val);
if ($val === 'B') {
$vec[$key] = 'D';
}
var_dump($vec);
}
}
run();
Output
array(3) {
[0]=>
string(1) "A"
[1]=>
string(1) "B"
[2]=>
string(1) "C"
}
string(1) "A"
array(3) {
[0]=>
&string(1) "A"
[1]=>
string(1) "B"
[2]=>
string(1) "C"
}
string(1) "B"
array(3) {
[0]=>
string(1) "A"
[1]=>
&string(1) "D"
[2]=>
string(1) "C"
}
string(1) "C"
array(3) {
[0]=>
string(1) "A"
[1]=>
string(1) "D"
[2]=>
&string(1) "C"
}
object(HH\Vector)#1 (3) {
[0]=>
string(1) "A"
[1]=>
string(1) "B"
[2]=>
string(1) "C"
}
string(1) "A"
object(HH\Vector)#1 (3) {
[0]=>
string(1) "A"
[1]=>
string(1) "B"
[2]=>
string(1) "C"
}
string(1) "B"
object(HH\Vector)#1 (3) {
[0]=>
string(1) "A"
[1]=>
string(1) "D"
[2]=>
string(1) "C"
}
string(1) "C"
object(HH\Vector)#1 (3) {
[0]=>
string(1) "A"
[1]=>
string(1) "D"
[2]=>
string(1) "C"
}
基本上,您明確拉出集合的鍵/值對,然后顯式修改鍵的值。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: