如何在php而不是默认哈希中使用密钥重复创建数据结构?(How to create data structure with key duplicates in php instead of defau

编程入门 行业动态 更新时间:2024-10-24 20:11:09
如何在php而不是默认哈希中使用密钥重复创建数据结构?(How to create data structure with key duplicates in php instead of default hash?)

我想创建包装类,它将启用密钥重复,而默认哈希不允许它。 类应该使用php5中引入的成员重载机制,因此它会模仿标准哈希所具有的所有行为。 例如,我想要像smth一样

$var => obj( :values_arr -> array( obj(:key -> 'mykey', :value -> 'val1'), obj(:key -> 'mykey', :value -> 'val2') ) )

如果我想得到$ var ['mykey'],它应该返回数组('val1','val2'),但如果我想用新的'mykey'=>'value'对扩展obj,我会调用

$val['mykey'][] = 'value'

主要思想是保留了散列的行为,并且在尝试将值赋值给使用现有键之后,它不会被覆盖,而是附加到列表中。

你会如何模仿php5中的其他数据结构(5.3之前)? 您想分享任何已知的解决方案或示例吗?

I want to create wrapper class, which will enable keys duplicates while default hash does not allow it. Class should use member overloading mechanism introduced in php5, so it would imitate all the behavior standard hash has. For example, I want to have smth like

$var => obj( :values_arr -> array( obj(:key -> 'mykey', :value -> 'val1'), obj(:key -> 'mykey', :value -> 'val2') ) )

If I want to get $var['mykey'], it should return array('val1', 'val2'), but if I want to extend obj with new 'mykey' => 'value' pair, I would call

$val['mykey'][] = 'value'

Main idea is that behavior of the hash was preserved and after attempt to assign value to using existing key, it wouldn't be overwritten, but appended to the list.

How would you imitate other data structures in php5 (before 5.3)? Are there any known solutions or examples you want to share?

最满意答案

喜欢这个

class MultiMap { protected $map = array(); function __set($key, $val) { if(!isset($this->map[$key])) return $this->map[$key] = $val; if(!is_array($this->map[$key])) $this->map[$key] = array($this->map[$key]); $this->map[$key][] = $val; } function __get($key) { return $this->map[$key]; } } $m = new MultiMap; $m->foo = 1; $m->foo = 2; $m->bar = 'zzz'; print_r($m->foo); print_r($m->bar);

但整个想法对我来说有点奇怪。 你能解释一下为什么需要这个吗?

我不清楚为什么你需要运算符作为你的AST键,也许这样的结构会更方便

('op' => 'AND', 'args' => [ (op => AND, args => [ (op => atom, value => word1), (op => atom, value => word2), ]), (op => AND, args => [ (op => atom, value => word3), (op => atom, value => word4), ]) ])

like this

class MultiMap { protected $map = array(); function __set($key, $val) { if(!isset($this->map[$key])) return $this->map[$key] = $val; if(!is_array($this->map[$key])) $this->map[$key] = array($this->map[$key]); $this->map[$key][] = $val; } function __get($key) { return $this->map[$key]; } } $m = new MultiMap; $m->foo = 1; $m->foo = 2; $m->bar = 'zzz'; print_r($m->foo); print_r($m->bar);

but the whole idea looks a bit odd to me. Can you explain why you need this?

it's not clear for me why you need operators as keys in your AST perhaps a structure like this would be more convenient

('op' => 'AND', 'args' => [ (op => AND, args => [ (op => atom, value => word1), (op => atom, value => word2), ]), (op => AND, args => [ (op => atom, value => word3), (op => atom, value => word4), ]) ])

更多推荐

本文发布于:2023-07-29 20:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319579.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:密钥   数据结构   而不是   如何在   create

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!