与数字字符串键阵列Diddling [英] Diddling with arrays with numeric string keys

查看:96
本文介绍了与数字字符串键阵列Diddling的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要了解为什么我问这个,请参阅并根据它的意见。请看下面的code:

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);

产地:

array(1) {
  ["10"]=>
  string(5) "Thing"
}

现在,我无法访问通过 $ objArray ['10'] ,因为PHP转换数字字符串键整数键。该手册明确规定整数属性不可访问在铸造一个数组的对象。还是他们?

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;
            }
        }
    }
}

现在,我能做的(演示)

$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 键()的next()等,但它不会工作。我不能找到一种方法取消设置的。

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.

推荐答案

您可以尝试使用删除array_splice(),如果你知道它的偏移量(的foreach(...){$抵消++; ...} ),但存储这样的一个数组中的数据,实在不是一个好主意。您应该将这些对象转换为数组用foreach:

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;

这篇关于与数字字符串键阵列Diddling的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆