dcsimg
LinkedIn
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Using Iterators in PHP : Page 2

Discover how to use the iterators in the Standard PHP Library


advertisement

Listing Classes and Methods with Reflection

As an interesting exercise, you can view a full list of iterator classes and their methods by using the Reflection API, produced by the following short application. Listing 1 shows the output:

<?php
  //Using the Reflection API to see a full list of methods of the      
  //DirectoryIterator class
  Reflection::export(new ReflectionClass('DirectoryIterator'));
?>

The FilterIterator Class

You use this class to filter out unwanted values using the accept() method. The FilterIterator class is not meant to be used directly; instead, you need to extend it to implement custom iterator filters. In particular, you must implement the accept() method.

The following application iterates over the $flowers array, filtering it with the accept() method to list only numeric array key values:

<?php
$flowers = array('tulip', 'a'=>'rose', 'lily', 
   'b'=>'snowdrop', 'crocus');
class FlowerClass extends FilterIterator{
   public function __construct( Iterator $iterator ){
     parent::__construct( $iterator );
   }
   //This function checks if the array keys are numeric
   function accept(){
     return is_numeric($this->key());
   }
}
//Create a new ArrayIterator instance and pass it to the FlowerClass 
//constructor to create a new FlowerClass object
$value = new FlowerClass(new ArrayIterator($flowers));
echo 'List all the elements from the $flowers array that 
   have numeric keys: '.'<br />';
//Iterate over the values in the ArrayIterator
foreach($value as $key=>$value){
   echo $key.' =>'.$value.'<br />';
}
?>

The output is:

List all the elements from the $flowers array that have numeric keys: 
0 =>tulip
1 =>lily
2 =>crocus

Here's an alternate version that iterates the $flowers array, listing only the odd array key values:

<?php
$flowers = array('tulip', 'rose', 'lily', 'snowdrop', 'crocus');
class MyClass extends FilterIterator{
   public function __construct( Iterator $iterator ){
      parent::__construct( $iterator );
   }
   //This function checks if the array keys are odd numbers
   function accept(){  
      if(($this->key() % 2) != 0){
         return $this->key();
      }
   }
}
//Create a new ArrayIterator instance and 
//pass it to the MyClass constructor 
$value = new MyClass(new ArrayIterator($flowers));
echo 'List all the $flowers elements with odd numbered keys: '.'<br />';
//Iterate over the $flowers array 
foreach($value as $key=>$value){
    echo $key.' == '.$value.'<br />';
}
?>

The output is:

'List all the $flowers elements with odd numbered keys: 
1 == rose
3 == snowdrop

The DirectoryIterator Class

The DirectoryIterator class provides a simple interface for viewing the contents of filesystem directories. The following example lists the name and size of files in a specific directory:

<?php
//Create a new DirectoryIterator instance
$path = new DirectoryIterator('../Iterator');
//Iterate over the files in the directory, listing the name 
and  ize of the encountered filessda
foreach ($path as $file) {
    // Return file name of current DirectoryIterator item
    echo $file->getFilename() . "<br />";
    // Get size of current DirectoryIterator item
    echo $file->getSize() . "<br />";
    }
?>

Here's the output:

The filename = iterator_1.php
The file size = 861
The filename = iterator_11.php
The file size = 484
The filename = iterator_2.php
The file size = 648
The filename = iterator_3.php
The file size = 403


Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date