Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /share/CACHEDEV1_DATA/Web/www/libraries/UBBcode/text_parser.class.php on line 228
Recursed Iterators

Comments Blog About Development Research Sites

Recursed Iterators

Jan 17, 2008
Okay, that pun in the title might be a bit too obvious. Or not at all to a non-programmer of course, though undoubtedly those that are will agree. Recursive iterators tend to be a curse.

Now, to illustrate that point: I just wrote a very lengthy article on them, only to see it disapear when my session expired in the meantime. See what I mean? ;)

Okay, longer story short: I just had to write another iterator a short while ago and managed to do it in just onder 20 lines - a new personal record - so here it is!

Code (php) (nieuw venster):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class DataParser {

  private
static $path,
                 $entries;

  public function
__construct (array $array) {
    foreach(
$array as $key => $value) {
      if(
is_array($value)) {
        $oldPath     = self::$path;
        self::$path .= '\\' . $key;
        new
AdminSpiderDataParser($value);
        self::$path  = $oldPath;
      }
      else

        self::$entries[self::$path . '\\' . $key] = $value;
    }
  }        
      
  public function
retrieve () {
    return
self::$entries;
  }
}


What this does is take an array and retrieve the (x-) path to each key in it and put it in one associative array. Quite usefull if you want to display a list of all items in for example an XML document, together with their path :)

FragFrog out!

New comment

Your name:
Comment: