<?php
$object1 = (object)['a' => 1];
$object2 = (object)['a' => 2];
$ref1 = [];
$ref2 = [];
foreach ([$object1, $object2] as $k) {
$ref1[$k->a] = &$k;
$ref2[$k->a] = $k;
}
dump(compact('ref1', 'ref2'));
$object1 = (object)['a' => 1];
$object2 = (object)['a' => 2];
$ref1 = [];
$ref2 = [];
foreach ([$object1, $object2] as $k) {
$ref1[$k->a] = &$k;
$ref2[$k->a] = $k;
}
dump(compact('ref1', 'ref2'));
输出结果:
array:2 [
"ref1" => array:2 [
1 => &1 {#583
+"a": 2
}
2 => &1 {#583}
]
"ref2" => array:2 [
1 => {#579
+"a": 1
}
2 => {#583}
]
]
"ref1" => array:2 [
1 => &1 {#583
+"a": 2
}
2 => &1 {#583}
]
"ref2" => array:2 [
1 => {#579
+"a": 1
}
2 => {#583}
]
]
当使用 &$k
时获取的为循环$k
的引用,如果使用 as &$k
又是另一个操作了。只能说,历史代码的坑挺多。
当前还没有任何评论