Packages » Package util » Class ComboIterable

Class ComboIterable

Declaration:
<? class ComboIterable implements Iterable ?>

Description:
This class allows you to combine several iterables into one so that you iterate them in parallel. The result of a call to ComboIterable::getNext() will be an array that holds key=>value pairs combined from the results of calls to Iterable::getNext() methods of each iterable contained by this ComboIterable. The primary use of this class is allowing to iterate over keys and values of a single array so that each call to ComboIterable::getNext will return an array containing current key and value. The keys of this resulting array will be fixed like 'key' and 'value' (you can specify them when instantiating a ComboIterable). The usage of this class is best illustrated by the following example:
<?

 $a 
= array('one'=>'1''two'=>'2''three'=>'3'); // We will iterate over key=>value pairs of this array
 
$ci = new ComboIterable(array('key'array_keys($a)), array('val'array_values($a)));
 while(
$ci->hasMore()) {
   
$r $ci->getNext();
   echo 
$r['key'], '='$r['val'], "\n";
 }
 
?>
This snippet will print
 one=1
 two=2
 three=3
 
Such approach is very convenient when you need to display keys and values of a single array with the HTMLShowIterable tag - if this tag iterates over an iterable that returns scalar values, it is impossible to access array keys from the template; only values are available as the template variable html.showiterable.value. Using ComboIterable you can easily loop through array keys and values, as well as combine more parallel arrays into one Iterable. You can also use iterables that return arrays with this class. In this case just pass the iterables themselves to the constructor. For example:
<?

 $a1 
= new IterableArray(array(array('alpha'=>1), array('alpha'=>2), array('alpha'=>3)));
 
$a2 = new IterableArray(array(array('beta'=>10), array('beta'=>20), array('beta'=>30)));
 
$ci = new ComboIterable($a1$a2);
 while(
$ci->hasMore()) {
   
$r $ci->getNext();
   echo 
$r['alpha'], ', '$r['beta'], "\n";
 }
 
?>
produces
 1, 10
 2, 20
 3, 30
 


Implemented Interfaces:
Iterable

Authors:
  • Dennis Popel

Methods:
  • __construct()
    Construct a ComboIterable from arbitrary number of Iterables
  • getCount()
    Get the total number of items in this iterable
  • hasMore()
    Return true if more elements available
  • next()
    Return next element and advance pointer
  • reset()
    Rewind this iterable to the before-the-first position. A call to the Iterable::getNext will fetch the first item. Note that in PaginatedIterable this method will rewind to the first item on the current page. Note: In order to avoid name collision method name has been changed. Previous name was "rewind"