From: Florent Bruneau Date: Sat, 14 Feb 2009 17:43:32 +0000 (+0100) Subject: Add a 'flat' mode to PlArrayIterator. X-Git-Tag: core/1.0.1~19 X-Git-Url: http://git.polytechnique.org/?a=commitdiff_plain;h=e94c4a1713cc0d63508a5d0ecd2d572f047975fb;p=platal.git Add a 'flat' mode to PlArrayIterator. Signed-off-by: Florent Bruneau --- diff --git a/classes/pliteratorutils.php b/classes/pliteratorutils.php index f4ce30d..0d67ac5 100644 --- a/classes/pliteratorutils.php +++ b/classes/pliteratorutils.php @@ -28,9 +28,9 @@ class PlIteratorUtils * array(key => array(0 => key_for_depth0 [, 1 => key_for_depths1, ...]), * value => the value); */ - public static function fromArray(array $array, $depth = 1) + public static function fromArray(array $array, $depth = 1, $flat = false) { - return new PlArrayIterator($array, $depth); + return new PlArrayIterator($array, $depth, $flat); } @@ -75,8 +75,9 @@ class PlArrayIterator implements PlIterator private $_first; private $_last; private $_pos; + private $_flat; - public function __construct(array &$array, $depth = 1) + public function __construct(array &$array, $depth = 1, $flat = false) { $this->array =& $array; $this->depth = $depth; @@ -84,6 +85,7 @@ class PlArrayIterator implements PlIterator $this->_pos = 0; $this->_first = false; $this->_last = false; + $this->_flat = $flat; for ($i = 0 ; $i < $depth ; ++$i) { if ($i == 0) { @@ -146,8 +148,12 @@ class PlArrayIterator implements PlIterator $keys[] = key($this->_its[$i]); } next($this->_its[$this->depth - 1]); - return array('keys' => $keys, - 'value' => $val); + if ($this->_flat) { + return $val; + } else { + return array('keys' => $keys, + 'value' => $val); + } } public function total()