Edit report at http://bugs.php.net/bug.php?id=45684&edit=1
ID: 45684 Comment by: info at strictcoding dot co dot uk Reported by: puts dot email at gmail dot com Summary: A request for foreach to be key-type agnostic. Status: Open Type: Feature/Change Request Package: Feature/Change Request Operating System: N/A PHP Version: 5.3.0alpha1 Block user comment: N Private report: N New Comment: Agreed, it would be a very nice addition, this is currently a blocking issue for doing some cool stuff. Previous Comments: ------------------------------------------------------------------------ [2010-09-23 11:22:06] mep_eisen at web dot de I don't get the point why ArrayAccess allowes object as keys (which is very nice) but Iterator does not allow it. I use proxy objects to store instances during serialization and connect them to a single object instance during unserialization. Having those proxies as keys in arrays would be really nice. SplObjectStorage does not work because multiple proxies that are connected to the same underlying object instance will always be different keys for SplObjectStorage. What's about the feature request? Any news? ------------------------------------------------------------------------ [2010-01-21 19:19:04] bytebrite at gmail dot com This would be a nice addition. What technical issues are preventing this functionality from being realized? ------------------------------------------------------------------------ [2009-12-16 02:29:33] tejas dot net+php at gmail dot com Agreed. Its cool that 5.3 has the ArrayAccess interface, but key() not being able to return objects really gets in the way. ------------------------------------------------------------------------ [2009-03-12 17:38:25] fqqdk at freemail dot hu I don't see why ArrayAccess can allow objects (or anything) as array keys, and Iterator doesn't. This is inconsistent. ------------------------------------------------------------------------ [2008-08-02 00:57:22] puts dot email at gmail dot com Description: ------------ A request for foreach to be key-type agnostic (ideally, assoc's would be) so that the following is possible: <?php class Hash implements ArrayAccess, Iterator { private $key = array(); private $val = array(); public function offsetGet($key) { $offset = array_search($key, $this->key); return $this->val[$offset]; } public function offsetSet($key, $val) { $offset = array_search($key, $this->key); if ($offset === false) { $this->key[] = $key; $this->val[] = $val; } else { $this->val[$offset] = $val; } } public function offsetExists($key) { return in_array($key, $this->key); } public function offsetUnset($key) { $offset = array_search($key, $this->key); unset($this->key[$offset], $this->val[$offset]); } public function rewind() { reset($this->key); reset($this->val); } public function key() { return current($this->key); } public function current() { return current($this->val); } public function next() { next($this->key); return next($this->val); } public function valid() { return is_int(key($this->key)); } } $h = new hash; $o0 = new stdclass; $o1 = new stdclass; $o2 = new stdclass; $o3 = new stdclass; foreach (array($o0, $o1, $o2, $o3) as $i => $o) { $o->name = "o" . $i; } $h[$o0] = $o1; $h[$o1] = $o2; $h[$o2] = $o3; foreach ($h as $key => $val) {} ?> Reproduce code: --------------- See description. Expected result: ---------------- foreach ($h as $key => $val) { # $key === $o[012] # $val === $o[123] } Actual result: -------------- foreach ($h as $key => $val) { # Warning: Illegal type returned from Hash::key() # $key === 0 # $val === $o[123] } ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=45684&edit=1