php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #45684 A request for foreach to be key-type agnostic.
Submitted: 2008-08-02 00:57 UTC Modified: 2013-03-15 07:00 UTC
Votes:26
Avg. Score:4.8 ± 0.4
Reproduced:20 of 21 (95.2%)
Same Version:9 (45.0%)
Same OS:10 (50.0%)
From: puts dot email at gmail dot com Assigned: nikic (profile)
Status: Closed Package: *General Issues
PHP Version: 5.3.0alpha1 OS: N/A
Private report: No CVE-ID: None
 [2008-08-02 00:57 UTC] 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]
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-03-12 17:38 UTC] 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.
 [2009-12-16 02:29 UTC] 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.
 [2010-01-21 19:19 UTC] bytebrite at gmail dot com
This would be a nice addition. What technical issues are preventing this functionality from being realized?
 [2010-09-23 11:22 UTC] 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?
 [2011-04-05 19:26 UTC] info at strictcoding dot co dot uk
Agreed, it would be a very nice addition, this is currently a blocking issue for 
doing some cool stuff.
 [2012-06-05 22:24 UTC] nikic@php.net
Assigning to Etienne as he's working on this.
 [2012-06-05 22:24 UTC] nikic@php.net
-Status: Open +Status: Assigned -Package: Feature/Change Request +Package: *General Issues -Assigned To: +Assigned To: colder
 [2013-02-24 03:04 UTC] stoffle at gmail dot com
Is there any chance this might make it into 5.5?
 [2013-02-24 11:00 UTC] nikic@php.net
@stoffle: I have an RFC and patch here: https://wiki.php.net/rfc/foreach-non-scalar-keys. I'll try to get it into 5.5.
 [2013-02-24 11:00 UTC] nikic@php.net
-Assigned To: colder +Assigned To: nikic
 [2013-02-24 23:17 UTC] stoffle at gmail dot com
That's great, thanks for the update. I can now also remove my botched attempt from 
GitHub :)
 [2013-03-15 07:00 UTC] nikic@php.net
-Status: Assigned +Status: Closed
 [2013-03-15 07:00 UTC] nikic@php.net
The RFC has been accepted, commited here: https://github.com/php/php-src/commit/fcc6611de9054327441786e52444b5f8eecdd525 (for PHP 5.5)
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sat Aug 15 18:01:26 2020 UTC