要了解我为什么要问这个问题,请阅读此内容及其下方的评论。考虑以下代码:
$obj = new stdClass;
$obj->{10} = 'Thing';
$objArray = (array) $obj;
var_dump($objArray);
产生:
array(1) {
["10"]=>
string(5) "Thing"
}
现在,我无法访问它,$objArray['10']
因为 PHP 将数字字符串键转换为整数键。该手册在将数组转换为对象时明确指出“整数属性不可访问”。还是他们?
为了证明文档是错误的,我创建了一个类:
class strKey implements ArrayAccess
{
private $arr;
public function __construct(&$array)
{
$this->arr = &$array;
}
public function offsetExists($offset)
{
foreach ($this->arr as $key => $value)
{
if ($key == $offset)
{
return true;
}
}
return false;
}
public function offsetGet($offset)
{
foreach ($this->arr as $key => $value)
{
if ($key == $offset)
{
return $value;
}
}
return null;
}
public function offsetSet($offset, $value)
{
foreach($this->arr as $key => &$thevalue)
{
if ($key == $offset)
{
$thevalue = $value;
return;
}
}
// if $offset is *not* present...
if ($offset === null)
{
$this->arr[] = $value;
}
else
{
// this won't work with string keys
$this->arr[$offset] = $value;
}
}
// can't implement this
public function offsetUnset($offset)
{
foreach ($this->arr as $key => &$value)
{
if ($key == $offset)
{
//$value = null;
}
}
}
}
现在,我可以做(演示):
$b = new strKey($objArray);
echo $b['10']; // Thing
$b['10'] = 'Something else';
// because the classes works with a reference to the original array,
// this will give us a modified array:
var_dump($objArray);
最后一个难题是,如何取消设置键为数字字符串的元素?我尝试使用ArrayIterator
, key()
,next()
等,但它不起作用。我找不到解除这些的方法。
任何解决方案都应该使用原始数组,而不是创建副本并替换原始数组。