To understand why I'm asking this, read this and the comments under it. Consider the following code:
$obj = new stdClass;
$obj->{10} = 'Thing';
$objArray = (array) $obj;
var_dump($objArray);
Produces:
array(1) {
["10"]=>
string(5) "Thing"
}
Now, I can't access that via $objArray['10'] because PHP converts numeric strings keys to integer keys. The manual explicitly states "integer properties are unaccessible" upon casting an array to an object. Or are they?
To prove the docs wrong, I created a class:
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;
}
}
}
}
Now, I can do (demo):
$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);
The final piece of the puzzle is, how do I unset an element whose key is a numeric string? I tried using ArrayIterator, key(), next(), etc. but it won't work. I can't find a way unset those.
Any solution should work with the original array, not create a copy and replace the original.