PHP映射键(数组)按数字删除';不起作用


PHP Map keys (arrays) Remove by numeric doesn't work?

这是我的Map类:

/**
 * Map
 *
 * Collection of items, with a key.
 * @Author Jony <artemkller@gmail.com>
 */
Class Map
{
    /**
     * Annonymous Constructor
     * Sets up the array.
     */
    function __construct()
    {
        $this->map = array();
    }
    /**
     * Method add
     * Adds a new item to the collection with a key.
     *
     * Note: Key can NOT be numeric!
     *
     * @param key       The key of the item.
     * @param value     The value of the item.
     * @return void
     */
    public function add($key, $value)
    {
        $this->map[$key] = $value;
    }
    /**
     * Contains
     * Checks if an item with specific key exists.
     *
     * @param key The key.
     * @return Boolean
     */
    public function contains($key)
    {
        if (!is_numeric($key))
        {
            return (isset($this->map[$key])) ? true : false;
        }
        else
        {
            $values = array_values($this->map);
            return (isset($values[$key])) ? true : false;
        }
    }
    /**
     * Method remove
     * Removes an item from the collection by key.
     *
     * @param key   The key of the item.
     * @return void
     */
    public function remove($key)
    {
        if (!is_numeric($key))
        {
            if (isset($this->map[$key]))
                unset($this->map[$key]);
        }
        else
        {
            $values = array_values($this->map);
            if (isset($values[$key]))
            {
                unset($values[$key]);
            }
        }
    }
    /**
     * Method get
     * Gets an item from the collection by key.
     *
     * Note: If entered numeric, it will get the key 
     * by it's offset position number. Arrays starting from 0.
     *
     * @param key The key of the item
     * @return Array item value (Either String, Integer, Object).
     */
    public function get($key)
    {
        if (!is_numeric($key))
        {
            return (isset($this->map[$key])) ? $this->map[$key] : null;
        }
        else
        {
            $values = array_values($this->map);
            return (isset($values[$key])) ? $values[$key] : null;
        }
    }
}

我想创建自己的Map类型数组类(来自Java),有时只是为了练习和使用。

现在我在contains/remove方法上遇到了问题。

当我不使用数字remove/contains时,一切都很好,错误主要是remove。

当我按偏移量(数字)删除一个项时,我认为它不会从实际数组中删除。

好的,现在让我们调试和测试它。

                $d = new Map();
                $d->add("heyy", 55);
                $d->remove(0);
                if ($d->contains(0))
                {
                    echo 5555;
                }

创建一个新的映射对象,添加一个新值,删除偏移量0,检查是否存在,如果存在echo 5555

现在,让我们执行var_dump($values[$key]);删除后:

结果:NULL

删除前:

结果:55

现在让我们在删除后回显$this->映射("heyy"):

结果:55

好的,现在这意味着该项不会从数组本身中删除。

我尝试过的:

            $values = array_values($this->map);
            if (isset($this->map[$values[$key]]))
            {
                unset($this->map[$values[$key]]);
            }

仍然不起作用。

这个怎么了

您需要使用array_keys(),而不是array_values(),因为您需要取消设置键,而不是值。示例:

$map = array('abc' => 'def');
var_dump($map);
array(1) {
  ["abc"]=>
  string(3) "def"
}
$keys = array_keys($map);
if (isset($map[$keys[0]])) {
    unset($map[$keys[0]]);
}
var_dump($map);
array(0) {
}
public function remove($key)
{
    // First convert numeric key to the actual key at that position
    if (is_numeric($key))
    {
        $keys = array_keys($this->map);
        $key = $keys[$key];
    }
    // Now remove the key
    unset($this->map[$key]);
}