当我这样做时:
<?php
$object = array(
'key' => 'value',
1 => 'value',
'11' => 'value',
'22' => 'value'
);
print_r($object); echo'<br>';
$object = (object)$object;
print_r($object); echo'<br>';echo'<br>';
//ok looks good so far but then
echo $object->key . '<br>'; // good
echo $object->{1} . '<br>'; // bad
echo $object->{11} . '<br>'; // bad
echo $object->{'22'} . '<br>'; // bad
$object->key = 'changed value';
$object->{1} = 'changed value';
$object->{'11'} = 'changed value';
$object->{'22'} = 'changed value';
echo'<br>'; print_r($object); echo'<br>';echo'<br>';
echo $object->key . '<br>'; // good
echo $object->{1} . '<br>'; // good
echo $object->{11} . '<br>'; // good
echo $object->{'22'} . '<br>'; // good
$object = (array)$object;
echo'<br>'; print_r($object);echo'<br>';echo'<br>';
echo $object['key'] . '<br>'; // good
echo $object[1] . '<br>'; // wtf
echo $object['11'] . '<br>'; // wtf
echo $object['22'] . '<br>'; // wtf
?>
我得到这个结果:
Array ( [key] => value [1] => value [11] => value [22] => value )
stdClass Object ( [key] => value [1] => value [11] => value [22] => value )
value
Notice: Undefined property: stdClass::$1 in C:\Users\wl\Documents\USBWebserver v8.5\USBWebserver v8.5\8.5\root\Client\test.php on line 19
Notice: Undefined property: stdClass::$11 in C:\Users\wl\Documents\USBWebserver v8.5\USBWebserver v8.5\8.5\root\Client\test.php on line 20
Notice: Undefined property: stdClass::$22 in C:\Users\wl\Documents\USBWebserver v8.5\USBWebserver v8.5\8.5\root\Client\test.php on line 21
stdClass Object ( [key] => changed value [1] => value [11] => value [22] => value [1] => changed value [11] => changed value [22] => changed value )
changed value
changed value
changed value
changed value
Array ( [key] => changed value [1] => value [11] => value [22] => value [1] => changed value [11] => changed value [22] => changed value )
changed value
value
value
value
这是 PHP 中的一个错误,为什么会发生这种情况。一个数组或对象如何有 2 个不同值的相等键!?