1

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.

2 Answers 2

3

You can try to remove it with array_splice(), if you know its offset ( foreach (...) { $offset++; ... } ), but storing data in an arrays like this, is really not a good idea. You should convert these objects to array with foreach:

foreach ( $obj as $key => $value )
    $array[$key] = $value;
Sign up to request clarification or add additional context in comments.

Comments

0

Following pozs's advice, here's the resulting offsetUnset(). I also added a new offsetSet() which supports adding numeric keys.

public function offsetUnset($offset)
{
    $off = 0;

    foreach ($this->arr as $key => $value)
    {
        if ($key === $offset)
        {
            array_splice($this->arr, $off, 1);
            return;
        }
        $off++;
    }       
}

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;
    }
    // it's a numeric string key, now we have to hack it
    else if (strval(intval($offset)) === $offset)
    {
        // create new array via loophole:
        $tempObj = new stdClass;
        $tempObj->{$offset} = $value;

        // append to old array (+= doesn't create copies)
        $this->arr += (array) $tempObj;
        unset($tempObj);
    }
    // we are dealing with a normal key
    else
    {
        $this->arr[$offset] = $value;
    }
}

I've officially defeated PHP. Yay!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.