From e94c4a1713cc0d63508a5d0ecd2d572f047975fb Mon Sep 17 00:00:00 2001 From: Florent Bruneau Date: Sat, 14 Feb 2009 18:43:32 +0100 Subject: [PATCH] Add a 'flat' mode to PlArrayIterator. Signed-off-by: Florent Bruneau --- classes/pliteratorutils.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) 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() -- 2.1.4