迭代器

目录

SPL 提供一系列迭代器以遍历不同的对象。

SPL Iterators Class Tree

  • ArrayIterator
    • RecursiveArrayIterator
  • EmptyIterator
  • IteratorIterator
    • AppendIterator
    • CachingIterator
      • RecursiveCachingIterator
    • FilterIterator
      • CallbackFilterIterator
        • RecursiveCallbackFilterIterator
      • RecursiveFilterIterator
        • ParentIterator
      • RegexIterator
        • RecursiveRegexIterator
    • InfiniteIterator
    • LimitIterator
    • NoRewindIterator
  • MultipleIterator
  • RecursiveIteratorIterator
    • RecursiveTreeIterator
  • DirectoryIterator (extends SplFileInfo)
    • FilesystemIterator
      • GlobIterator
      • RecursiveDirectoryIterator

简介

这个迭代器能陆续遍历几个迭代器

类摘要

AppendIterator

class AppendIterator extends IteratorIterator implements OuterIterator {

/* 方法 */

public __construct ( void )

public void append ( Iterator $iterator )

public mixed current ( void )

public ArrayIterator getArrayIterator ( void )

public Iterator getInnerIterator ( void )

public int getIteratorIndex ( void )

public scalar key ( void )

public void next ( void )

public void rewind ( void )

public bool valid ( void )

/* 继承的方法 */

public IteratorIterator::__construct ( Traversable $iterator )

public mixed IteratorIterator::current ( void )

public Traversable IteratorIterator::getInnerIterator ( void )

public mixed IteratorIterator::key ( void )

public void IteratorIterator::next ( void )

public void IteratorIterator::rewind ( void )

public bool IteratorIterator::valid ( void )

}

AppendIterator::append

Appends an iterator

说明

public void AppendIterator::append ( Iterator $iterator )

Appends an iterator.

参数

iterator
The iterator to append.

返回值

没有返回值。

范例

示例 #1 AppendIterator::append example

<?php
$array_a = new ArrayIterator(array('a', 'b', 'c'));
$array_b = new ArrayIterator(array('d', 'e', 'f'));

$iterator = new AppendIterator;
$iterator->append($array_a);
$iterator->append($array_b);

foreach ($iterator as $current) {
    echo $current;
}
?>

以上例程会输出:

abcdef

参见

  • AppendIterator::__construct

AppendIterator::__construct

Constructs an AppendIterator

说明

public AppendIterator::__construct ( void )

Constructs an AppendIterator.

参数

此函数没有参数。

返回值

没有返回值。

范例

示例 #1 Iterating AppendIterator with foreach

<?php
$pizzas   = new ArrayIterator(array('Margarita', 'Siciliana', 'Hawaii'));
$toppings = new ArrayIterator(array('Cheese', 'Anchovies', 'Olives', 'Pineapple', 'Ham'));

$appendIterator = new AppendIterator;
$appendIterator->append($pizzas);
$appendIterator->append($toppings);

foreach ($appendIterator as $key => $item) {
    echo $key . ' => ' . $item . PHP_EOL;
}
?>

以上例程会输出:

0 => Margarita
1 => Siciliana
2 => Hawaii
0 => Cheese
1 => Anchovies
2 => Olives
3 => Pineapple
4 => Ham

示例 #2 Iterating AppendIterator with the AppendIterator API

<?php
$pizzas   = new ArrayIterator(array('Margarita', 'Siciliana', 'Hawaii'));
$toppings = new ArrayIterator(array('Cheese', 'Anchovies', 'Olives', 'Pineapple', 'Ham'));

$appendIterator = new AppendIterator;
$appendIterator->append($pizzas);
$appendIterator->append($toppings);

while ($appendIterator->valid()) {
    printf(
        '%s => %s => %s%s',
        $appendIterator->getIteratorIndex(),
        $appendIterator->key(),
        $appendIterator->current(),
        PHP_EOL
    );
    $appendIterator->next();
}
?>

以上例程会输出:

0 => 0 => Margarita
0 => 1 => Siciliana
0 => 2 => Hawaii
1 => 0 => Cheese
1 => 1 => Anchovies
1 => 2 => Olives
1 => 3 => Pineapple
1 => 4 => Ham

注释

Caution

When using iterator_to_array to copy the values of the AppendIterator into an array, you have to set the optional use_key argument to FALSE. When use_key is not FALSE any keys reoccuring in inner iterators will get overwritten in the returned array. There is no way to preserve the original keys.

参见

  • AppendIterator::append

AppendIterator::current

Gets the current value

说明

public mixed AppendIterator::current ( void )

Gets the current value.

参数

此函数没有参数。

返回值

The current value if it is valid or NULL otherwise.

参见

  • Iterator::current
  • AppendIterator::key
  • AppendIterator::valid
  • AppendIterator::next
  • AppendIterator::rewind

AppendIterator::getArrayIterator

Gets the ArrayIterator

说明

public ArrayIterator AppendIterator::getArrayIterator ( void )

This method gets the ArrayIterator that is used to store the iterators added with AppendIterator::append.

参数

此函数没有参数。

返回值

Returns an ArrayIterator containing the appended iterators.

参见

  • AppendIterator::getInnerIterator

AppendIterator::getInnerIterator

Gets the inner iterator

说明

public Iterator AppendIterator::getInnerIterator ( void )

This method returns the current inner iterator.

参数

此函数没有参数。

返回值

The current inner iterator, or NULL if there is not one.

范例

示例 #1 AppendIterator::getInnerIterator example

<?php
$array_a = new ArrayIterator(array('a' => 'aardwolf', 'b' => 'bear', 'c' => 'capybara'));
$array_b = new RegexIterator($array_a, '/^[ac]/');

$iterator = new AppendIterator;
$iterator->append($array_a);
$iterator->append($array_b);

foreach ($iterator as $current) {
    $inner = $iterator->getInnerIterator();
    if ($inner instanceOf RegexIterator) {
        echo 'Filtered: ';
    } else {
        echo 'Original: ';
    }
    echo $current . PHP_EOL;
}
?>

以上例程会输出:

Original: aardwolf
Original: bear
Original: capybara
Filtered: aardwolf
Filtered: capybara

参见

  • AppendIterator::getIteratorIndex

AppendIterator::getIteratorIndex

Gets an index of iterators

说明

public int AppendIterator::getIteratorIndex ( void )

Gets the index of the current inner iterator.

参数

此函数没有参数。

返回值

Returns an integer, which is the zero-based index of the current inner iterator.

范例

示例 #1 AppendIterator.getIteratorIndex basic example

<?php
$array_a = new ArrayIterator(array('a' => 'aardwolf', 'b' => 'bear', 'c' => 'capybara'));
$array_b = new ArrayIterator(array('apple', 'orange', 'lemon'));

$iterator = new AppendIterator;
$iterator->append($array_a);
$iterator->append($array_b);

foreach ($iterator as $key => $current) {
    echo $iterator->getIteratorIndex() . '  ' . $key . ' ' . $current . PHP_EOL;
}
?>

以上例程会输出:

0  a aardwolf
0  b bear
0  c capybara
1  0 apple
1  1 orange
1  2 lemon

参见

  • AppendIterator::getInnerIterator
  • AppendIterator::getArrayIterator

AppendIterator::key

Gets the current key

说明

public scalar AppendIterator::key ( void )

Get the current key.

参数

此函数没有参数。

返回值

The current key if it is valid or NULL otherwise.

范例

示例 #1 AppendIterator::key basic example

<?php
$array_a = new ArrayIterator(array('a' => 'aardwolf', 'b' => 'bear', 'c' => 'capybara'));
$array_b = new ArrayIterator(array('apple', 'orange', 'lemon'));

$iterator = new AppendIterator;
$iterator->append($array_a);
$iterator->append($array_b);

// Manual iteration
$iterator->rewind();
while ($iterator->valid()) {
    echo $iterator->key() . ' ' . $iterator->current() . PHP_EOL;
    $iterator->next();
}

echo PHP_EOL;

// With foreach
foreach ($iterator as $key => $current) {
    echo $key . ' ' . $current . PHP_EOL;
}
?>

以上例程会输出:

a aardwolf
b bear
c capybara
0 apple
1 orange
2 lemon

a aardwolf
b bear
c capybara
0 apple
1 orange
2 lemon

参见

  • Iterator::key
  • AppendIterator::current
  • AppendIterator::valid
  • AppendIterator::next
  • AppendIterator::rewind

AppendIterator::next

Moves to the next element

说明

public void AppendIterator::next ( void )

Moves to the next element. If this means to another Iterator then it rewinds that Iterator.

参数

此函数没有参数。

返回值

没有返回值。

参见

  • Iterator::next
  • AppendIterator::current
  • AppendIterator::key
  • AppendIterator::valid
  • AppendIterator::rewind

AppendIterator::rewind

Rewinds the Iterator

说明

public void AppendIterator::rewind ( void )

Rewind to the first element of the first inner Iterator.

参数

此函数没有参数。

返回值

没有返回值。

参见

  • Iterator::rewind
  • AppendIterator::current
  • AppendIterator::key
  • AppendIterator::next
  • AppendIterator::valid

AppendIterator::valid

Checks validity of the current element

说明

public bool AppendIterator::valid ( void )

Checks validity of the current element.

参数

此函数没有参数。

返回值

Returns TRUE if the current iteration is valid, FALSE otherwise.

参见

  • AppendIterator::current
  • AppendIterator::key
  • AppendIterator::next
  • AppendIterator::rewind

简介

这个迭代器允许在遍历数组和对象时删除和更新值与键。

当你想多次遍历相同数组时你需要实例化 ArrayObject,然后让这个实例创建一个 ArrayIteratror 实例。 当你想遍历相同数组时多次你需要实例 ArrayObject 并且让这个实例创建一个 ArrayIteratror 实例,然后使用foreach 或者 手动调用 getIterator() 方法。

类摘要

ArrayIterator

class ArrayIterator implements ArrayAccess , SeekableIterator , Countable , Serializable {

/* 常量 */

const integer STD_PROP_LIST = 1 ;

const integer ARRAY_AS_PROPS = 2 ;

/* 方法 */

public void append ( mixed $value )

public void asort ( void )

public __construct ([ mixed $array = array() [, int $flags = 0 ]] )

public int count ( void )

public mixed current ( void )

public array getArrayCopy ( void )

public int getFlags ( void )

public mixed key ( void )

public void ksort ( void )

public void natcasesort ( void )

public void natsort ( void )

public void next ( void )

public bool offsetExists ( mixed $index )

public mixed offsetGet ( mixed $index )

public void offsetSet ( mixed $index , mixed $newval )

public void offsetUnset ( mixed $index )

public void rewind ( void )

public void seek ( int $position )

public string serialize ( void )

public void setFlags ( string $flags )

public void uasort ( callable $cmp_function )

public void uksort ( callable $cmp_function )

public void unserialize ( string $serialized )

public bool valid ( void )

}

预定义常量

ArrayIterator 标记

ArrayIterator::STD_PROP_LIST
Properties of the object have their normal functionality when accessed as list (var_dump, foreach, etc.).

ArrayIterator::ARRAY_AS_PROPS
可以通过属性访问条目(读写都支持)。

ArrayIterator::append

Append an element

说明

public void ArrayIterator::append ( mixed $value )

Appends value as the last element.

Warning

本函数还未编写文档,仅有参数列表。

参数

value
The value to append.

返回值

没有返回值。

注释

Note:

This method cannot be called when the class="classname">ArrayIterator refers to an class="type">object.

参见

  • ArrayIterator::next

ArrayIterator::asort

Sort array by values

说明

public void ArrayIterator::asort ( void )

Sorts an array by values.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

没有返回值。

参见

  • ArrayIterator::ksort
  • ArrayIterator::natcasesort
  • ArrayIterator::natsort

ArrayIterator::__construct

Construct an ArrayIterator

说明

public ArrayIterator::__construct ([ mixed $array = array() [, int $flags = 0 ]] )

Constructs an ArrayIterator object.

Warning

本函数还未编写文档,仅有参数列表。

参数

array
The array or object to be iterated on.

flags
Flags to control the behaviour of the ArrayIterator object. See ArrayIterator::setFlags.

返回值

An ArrayIterator object.

错误/异常

ArrayIterator::__construct throws an InvalidArgumentException if anything besides an array or an object is given.

参见

  • ArrayIterator::getArrayCopy

ArrayIterator::count

Count elements

说明

public int ArrayIterator::count ( void )

Gets the number of elements in the array, or the number of public properties in the object.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

The number of elements or public properties in the associated array or object, respectively.

参见

  • ArrayIterator::getFlags

ArrayIterator::current

Return current array entry

说明

public mixed ArrayIterator::current ( void )

Get the current array entry.

参数

此函数没有参数。

返回值

The current array entry.

范例

示例 #1 ArrayIterator::current example

<?php
$array = array('1' => 'one',
               '2' => 'two',
               '3' => 'three');

$arrayobject = new ArrayObject($array);

for($iterator = $arrayobject->getIterator();
    $iterator->valid();
    $iterator->next()) {

    echo $iterator->key() . ' => ' . $iterator->current() . "\n";
}
?>

以上例程会输出:

1 => one
2 => two
3 => three

ArrayIterator::getArrayCopy

Get array copy

说明

public array ArrayIterator::getArrayCopy ( void )

Get a copy of an array.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

A copy of the array, or array of public properties if ArrayIterator refers to an object.

参见

  • ArrayIterator::valid

ArrayIterator::getFlags

Get behavior flags

说明

public int ArrayIterator::getFlags ( void )

Gets the behavior flags of the ArrayIterator. See the ArrayIterator::setFlags method for a list of the available flags.

参数

此函数没有参数。

返回值

Returns the behavior flags of the ArrayIterator.

参见

  • >ArrayIterator::setFlags
  • ArrayIterator::valid

ArrayIterator::key

Return current array key

说明

public mixed ArrayIterator::key ( void )

This function returns the current array key

参数

此函数没有参数。

返回值

The current array key.

范例

示例 #1 ArrayIterator::key example

<?php
$array = array('key' => 'value');

$arrayobject = new ArrayObject($array);
$iterator = $arrayobject->getIterator();

echo $iterator->key(); //key
?>

ArrayIterator::ksort

Sort array by keys

说明

public void ArrayIterator::ksort ( void )

Sorts an array by the keys.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

没有返回值。

参见

  • ArrayIterator::asort
  • ArrayIterator::natcasesort
  • ArrayIterator::natsort

ArrayIterator::natcasesort

Sort an array naturally, case insensitive

说明

public void ArrayIterator::natcasesort ( void )

Sort the entries by values using a case insensitive "natural order" algorithm.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

没有返回值。

参见

  • ArrayIterator::asort
  • ArrayIterator::ksort
  • ArrayIterator::natsort
  • natcasesort

ArrayIterator::natsort

Sort an array naturally

说明

public void ArrayIterator::natsort ( void )

Sort the entries by values using "natural order" algorithm.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

没有返回值。

参见

  • ArrayIterator::asort
  • ArrayIterator::ksort
  • ArrayIterator::natcasesort
  • natsort

ArrayIterator::next

Move to next entry

说明

public void ArrayIterator::next ( void )

The iterator to the next entry.

参数

此函数没有参数。

返回值

没有返回值。

范例

示例 #1 ArrayIterator::next example

<?php
$arrayobject = new ArrayObject();

$arrayobject[] = 'zero';
$arrayobject[] = 'one';

$iterator = $arrayobject->getIterator();

while($iterator->valid()) {
    echo $iterator->key() . ' => ' . $iterator->current() . "\n";

    $iterator->next();
}
?>

以上例程会输出:

0 => zero
1 => one

ArrayIterator::offsetExists

Check if offset exists

说明

public bool ArrayIterator::offsetExists ( mixed $index )

Checks if the offset exists.

Warning

本函数还未编写文档,仅有参数列表。

参数

index
The offset being checked.

返回值

TRUE if the offset exists, otherwise FALSE

参见

  • ArrayIterator::valid

ArrayIterator::offsetGet

Get value for an offset

说明

public mixed ArrayIterator::offsetGet ( mixed $index )

Gets the value from the provided offset.

Warning

本函数还未编写文档,仅有参数列表。

参数

index
The offset to get the value from.

返回值

The value at offset index.

参见

  • ArrayIterator::offsetSet
  • ArrayIterator::offsetUnset

ArrayIterator::offsetSet

Set value for an offset

说明

public void ArrayIterator::offsetSet ( mixed $index , mixed $newval )

Sets a value for a given offset.

Warning

本函数还未编写文档,仅有参数列表。

参数

index
The index to set for.

newval
The new value to store at the index.

返回值

没有返回值。

参见

  • ArrayIterator::offsetGet
  • ArrayIterator::offsetUnset

ArrayIterator::offsetUnset

Unset value for an offset

说明

public void ArrayIterator::offsetUnset ( mixed $index )

Unsets a value for an offset.

Warning

本函数还未编写文档,仅有参数列表。

参数

index
The offset to unset.

返回值

没有返回值。

参见

  • ArrayIterator::offsetGet
  • ArrayIterator::offsetSet

ArrayIterator::rewind

Rewind array back to the start

说明

public void ArrayIterator::rewind ( void )

This rewinds the iterator to the beginning.

参数

此函数没有参数。

返回值

没有返回值。

范例

示例 #1 ArrayIterator::rewind example

<?php
$arrayobject = new ArrayObject();

$arrayobject[] = 'zero';
$arrayobject[] = 'one';
$arrayobject[] = 'two';

$iterator = $arrayobject->getIterator();

$iterator->next();
echo $iterator->key(); //1

$iterator->rewind(); //rewinding to the begining
echo $iterator->key(); //0
?>

ArrayIterator::seek

Seek to position

说明

public void ArrayIterator::seek ( int $position )

Warning

本函数还未编写文档,仅有参数列表。

参数

position
The position to seek to.

返回值

没有返回值。

ArrayIterator::serialize

Serialize

说明

public string ArrayIterator::serialize ( void )

Serialize.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

The serialized ArrayIterator.

参见

  • ArrayIterator::unserialize

ArrayIterator::setFlags

Set behaviour flags

说明

public void ArrayIterator::setFlags ( string $flags )

Set the flags that change the behavior of the ArrayIterator.

参数

flags
The new ArrayIterator behavior. It takes on either a bitmask, or named constants. Using named constants is strongly encouraged to ensure compatibility for future versions.

The available behavior flags are listed below. The actual meanings of these flags are described in the predefined constants.

valueconstant
1ArrayIterator::STD_PROP_LIST
2ArrayIterator::ARRAY_AS_PROPS

返回值

没有返回值。

参见

  • ArrayIterator::getFlags

ArrayIterator::uasort

Sort with a user-defined comparison function and maintain index association

说明

public void ArrayIterator::uasort ( callable $cmp_function )

This method sorts the elements such that indices maintain their correlation with the values they are associated with, using a user-defined comparison function.

Note:

If two members compare as equal, their relative order in the sorted array is undefined.

参数

cmp_function
在第一个参数小于,等于或大于第二个参数时,该比较函数必须相应地返回一个小于,等于或大于 0 的整数。

int callback ( mixed $a, mixed $b )

返回值

没有返回值。

参见

  • ArrayIterator::asort
  • ArrayIterator::uksort
  • usort

ArrayIterator::uksort

Sort by keys using a user-defined comparison function

说明

public void ArrayIterator::uksort ( callable $cmp_function )

This method sorts the elements by keys using a user-supplied comparison function.

Note:

If two members compare as equal, their relative order in the sorted array is undefined.

参数

cmp_function
在第一个参数小于,等于或大于第二个参数时,该比较函数必须相应地返回一个小于,等于或大于 0 的整数。

int callback ( mixed $a, mixed $b )

返回值

没有返回值。

参见

  • ArrayIterator::ksort
  • ArrayIterator::uasort
  • uksort

ArrayIterator::unserialize

Unserialize

说明

public void ArrayIterator::unserialize ( string $serialized )

Unserialize.

Warning

本函数还未编写文档,仅有参数列表。

参数

serialized
The serialized ArrayIterator object to be unserialized.

返回值

没有返回值。

参见

  • ArrayIterator::serialize

ArrayIterator::valid

Check whether array contains more entries

说明

public bool ArrayIterator::valid ( void )

Checks if the array contains any more entries.

参数

此函数没有参数。

返回值

Returns TRUE if the iterator is valid, otherwise FALSE

范例

示例 #1 ArrayIterator::valid example

<?php
$array = array('1' => 'one');

$arrayobject = new ArrayObject($array);
$iterator = $arrayobject->getIterator();

var_dump($iterator->valid()); //bool(true)

$iterator->next(); // advance to the next item

//bool(false) because there is only one array element
var_dump($iterator->valid());
?>

简介

This object supports cached iteration over another iterator.

类摘要

CachingIterator

class CachingIterator extends IteratorIterator implements OuterIterator , ArrayAccess , Countable {

/* 常量 */

const integer CachingIterator::CALL_TOSTRING = 1 ;

const integer CachingIterator::CATCH_GET_CHILD = 16 ;

const integer CachingIterator::TOSTRING_USE_KEY = 2 ;

const integer CachingIterator::TOSTRING_USE_CURRENT = 4 ;

const integer CachingIterator::TOSTRING_USE_INNER = 8 ;

const integer CachingIterator::FULL_CACHE = 256 ;

/* 方法 */

public __construct ( Iterator $iterator [, int $flags = self::CALL_TOSTRING ] )

public int count ( void )

public mixed current ( void )

public array getCache ( void )

public int getFlags ( void )

public Iterator getInnerIterator ( void )

public void hasNext ( void )

public scalar key ( void )

public void next ( void )

public void offsetExists ( mixed $index )

public void offsetGet ( string $index )

public void offsetSet ( mixed $index , mixed $newval )

public void offsetUnset ( string $index )

public void rewind ( void )

public void setFlags ( int $flags )

public void __toString ( void )

public void valid ( void )

}

预定义常量

CachingIterator::CALL_TOSTRING
Convert every element to string.

CachingIterator::CATCH_GET_CHILD
Don't throw exception in accessing children.

CachingIterator::TOSTRING_USE_KEY
Use key for conversion to string.

CachingIterator::TOSTRING_USE_CURRENT
Use current for conversion to string.

CachingIterator::TOSTRING_USE_INNER
Use inner for conversion to string.

CachingIterator::FULL_CACHE
Cache all read data.

CachingIterator::__construct

Construct a new CachingIterator object for the iterator

说明

public CachingIterator::__construct ( Iterator $iterator [, int $flags = self::CALL_TOSTRING ] )

Warning

本函数还未编写文档,仅有参数列表。

参数

iterator
Iterator to cache

flags
Bitmask of flags.

CachingIterator::count

The number of elements in the iterator

说明

public int CachingIterator::count ( void )

Warning

本函数还未编写文档,仅有参数列表。

May return the number of elements in the iterator.

参数

此函数没有参数。

返回值

The count of the elements iterated over.

CachingIterator::current

Return the current element

说明

public mixed CachingIterator::current ( void )

Warning

本函数还未编写文档,仅有参数列表。

May return the current element in the iteration.

参数

此函数没有参数。

返回值

Mixed

参见

  • Iterator::current

CachingIterator::getCache

Retrieve the contents of the cache

说明

public array CachingIterator::getCache ( void )

Retrieve the contents of the cache.

Note:

The CachingIterator::FULL_CACHE flag must be being used.

参数

此函数没有参数。

返回值

An array containing the cache items.

错误/异常

Throws a BadMethodCallException when the CachingIterator::FULL_CACHE flag is not being used.

范例

示例 #1 CachingIterator::getCache example

<?php
$iterator = new ArrayIterator(array(1, 2, 3));
$cache    = new CachingIterator($iterator, CachingIterator::FULL_CACHE);

$cache->next();
$cache->next();
var_dump($cache->getCache());

$cache->next();
var_dump($cache->getCache());
?>

以上例程会输出:

array(2) {
  [0]=>
  int(1)
  [1]=>
  int(2)
}
array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

CachingIterator::getFlags

Get flags used

说明

public int CachingIterator::getFlags ( void )

Warning

本函数还未编写文档,仅有参数列表。

Get the bitmask of the flags used for this CachingIterator instance.

参数

此函数没有参数。

返回值

Description...

CachingIterator::getInnerIterator

Returns the inner iterator

说明

public Iterator CachingIterator::getInnerIterator ( void )

Warning

本函数还未编写文档,仅有参数列表。

Returns the iterator sent to the constructor.

参数

此函数没有参数。

返回值

Returns an object implementing the Iterator interface.

CachingIterator::hasNext

Check whether the inner iterator has a valid next element

说明

public void CachingIterator::hasNext ( void )

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

CachingIterator::key

Return the key for the current element

说明

public scalar CachingIterator::key ( void )

Warning

本函数还未编写文档,仅有参数列表。

This method may return a key for the current element.

参数

此函数没有参数。

CachingIterator::next

Move the iterator forward

说明

public void CachingIterator::next ( void )

Warning

本函数还未编写文档,仅有参数列表。

Move the iterator forward.

参数

此函数没有参数。

返回值

没有返回值。

CachingIterator::offsetExists

The offsetExists purpose

说明

public void CachingIterator::offsetExists ( mixed $index )

Warning

本函数还未编写文档,仅有参数列表。

参数

index
The index being checked.

返回值

Returns TRUE if an entry referenced by the offset exists, FALSE otherwise.

CachingIterator::offsetGet

The offsetGet purpose

说明

public void CachingIterator::offsetGet ( string $index )

Warning

本函数还未编写文档,仅有参数列表。

参数

index
Description...

返回值

Description...

CachingIterator::offsetSet

The offsetSet purpose

说明

public void CachingIterator::offsetSet ( mixed $index , mixed $newval )

Warning

本函数还未编写文档,仅有参数列表。

参数

index
The index of the element to be set.

newval
The new value for the index.

返回值

没有返回值。

CachingIterator::offsetUnset

The offsetUnset purpose

说明

public void CachingIterator::offsetUnset ( string $index )

Warning

本函数还未编写文档,仅有参数列表。

参数

index
The index of the element to be unset.

返回值

没有返回值。

CachingIterator::rewind

Rewind the iterator

说明

public void CachingIterator::rewind ( void )

Warning

本函数还未编写文档,仅有参数列表。

Rewind the iterator.

参数

此函数没有参数。

返回值

没有返回值。

CachingIterator::setFlags

The setFlags purpose

说明

public void CachingIterator::setFlags ( int $flags )

Warning

本函数还未编写文档,仅有参数列表。

Set the flags for the CachingIterator object.

参数

flags
Bitmask of the flags to set.

返回值

没有返回值。

CachingIterator::__toString

Return the string representation of the current element

说明

public void CachingIterator::__toString ( void )

Warning

本函数还未编写文档,仅有参数列表。

Get the string representation of the current element.

参数

此函数没有参数。

返回值

The string representation of the current element.

CachingIterator::valid

Check whether the current element is valid

说明

public void CachingIterator::valid ( void )

Warning

本函数还未编写文档,仅有参数列表。

Check whether the current element is valid.

参数

此函数没有参数。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

简介

类摘要

CallbackFilterIterator

class CallbackFilterIterator extends FilterIterator implements OuterIterator {

/* 方法 */

public __construct ( Iterator $iterator , callable $callback )

public bool accept ( void )

/* 继承的方法 */

public abstract bool FilterIterator::accept ( void )

public FilterIterator::__construct ( Iterator $iterator )

public mixed FilterIterator::current ( void )

public Iterator FilterIterator::getInnerIterator ( void )

public mixed FilterIterator::key ( void )

public void FilterIterator::next ( void )

public void FilterIterator::rewind ( void )

public bool FilterIterator::valid ( void )

}

范例

The callback should accept up to three arguments: the current item, the current key and the iterator, respectively.

示例 #1 Available callback arguments

<?php

/**
 * Callback for CallbackFilterIterator
 *
 * @param $current   Current item's value
 * @param $key       Current item's key
 * @param $iterator  Iterator being filtered
 * @return boolean   TRUE to accept the current item, FALSE otherwise
 */
function my_callback($current, $key, $iterator) {
    // Your filtering code here
}

?>

Any callable may be used; such as a string containing a function name, an array for a method, or an anonymous function.

示例 #2 Callback basic examples

<?php

$dir = new FilesystemIterator(__DIR__);

// Filter large files ( > 100MB)
function is_large_file($current) {
    return $current->isFile() && $current->getSize() > 104857600;
}
$large_files = new CallbackFilterIterator($dir, 'is_large_file');

// Filter directories
$files = new CallbackFilterIterator($dir, function ($current, $key, $iterator) {
    return $current->isDir() && ! $iterator->isDot();
});

?>

CallbackFilterIterator::accept

Calls the callback with the current value, the current key and the inner iterator as arguments

说明

public bool CallbackFilterIterator::accept ( void )

This method calls the callback with the current value, current key and the inner iterator.

The callback is expected to return TRUE if the current item is to be accepted, or FALSE otherwise.

参数

此函数没有参数。

返回值

Returns TRUE to accept the current item, or FALSE otherwise.

参见

CallbackFilterIterator::__construct

Create a filtered iterator from another iterator

说明

public CallbackFilterIterator::__construct ( Iterator $iterator , callable $callback )

Creates a filtered iterator using the callback to determine which items are accepted or rejected.

参数

iterator
The iterator to be filtered.

callback
The callback, which should return TRUE to accept the current item or FALSE otherwise. See Examples.

May be any valid callable value.

返回值

没有返回值。

参见

简介

The DirectoryIterator class provides a simple interface for viewing the contents of filesystem directories.

类摘要

DirectoryIterator

class DirectoryIterator extends SplFileInfo implements SeekableIterator {

/* 方法 */

public __construct ( string $path )

public DirectoryIterator current ( void )

public int getATime ( void )

public string getBasename ([ string $suffix ] )

public int getCTime ( void )

public string getExtension ( void )

public string getFilename ( void )

public int getGroup ( void )

public int getInode ( void )

public int getMTime ( void )

public int getOwner ( void )

public string getPath ( void )

public string getPathname ( void )

public int getPerms ( void )

public int getSize ( void )

public string getType ( void )

public bool isDir ( void )

public bool isDot ( void )

public bool isExecutable ( void )

public bool isFile ( void )

public bool isLink ( void )

public bool isReadable ( void )

public bool isWritable ( void )

public string key ( void )

public void next ( void )

public void rewind ( void )

public void seek ( int $position )

public string __toString ( void )

public bool valid ( void )

}

更新日志

版本说明
5.1.2DirectoryIterator extends SplFileInfo.

DirectoryIterator::__construct

Constructs a new directory iterator from a path

说明

public DirectoryIterator::__construct ( string $path )

Constructs a new directory iterator from a path.

参数

path
The path of the directory to traverse.

错误/异常

Throws an UnexpectedValueException if the path cannot be opened.

Throws a RuntimeException if the path is an empty string.

更新日志

版本说明
5.3.0Throws UnexpectedValueException if the path cannot be opened.
5.1.3Throws RuntimeException if the path is an empty string.
5.1.0Throws RuntimeException on error. Previously, threw Exception.

范例

示例 #1 A DirectoryIterator::__construct example

This example will list the contents of the directory containing the script.

<?php
$dir = new DirectoryIterator(dirname(__FILE__));
foreach ($dir as $fileinfo) {
    if (!$fileinfo->isDot()) {
        var_dump($fileinfo->getFilename());
    }
}
?>

参见

  • SplFileInfo
  • Iterator

DirectoryIterator::current

Return the current DirectoryIterator item

说明

public DirectoryIterator DirectoryIterator::current ( void )

Get the current DirectoryIterator item.

参数

此函数没有参数。

返回值

The current DirectoryIterator item.

范例

示例 #1 A DirectoryIterator::current example

This example will list the contents of the directory containing the script.

<?php
$iterator = new DirectoryIterator(__DIR__);
while($iterator->valid()) {
    $file = $iterator->current();
    echo $iterator->key() . " => " . $file->getFilename() . "\n";
    $iterator->next();
}
?>

以上例程的输出类似于:

0 => .
1 => ..
2 => apple.jpg
3 => banana.jpg
4 => index.php
5 => pear.jpg

参见

  • DirectoryIterator::key
  • DirectoryIterator::next
  • DirectoryIterator::rewind
  • DirectoryIterator::valid
  • Iterator::current

DirectoryIterator::getATime

Get last access time of the current DirectoryIterator item

说明

public int DirectoryIterator::getATime ( void )

Get the last access time of the current DirectoryIterator item.

参数

此函数没有参数。

返回值

Returns the time the file was last accessed, as a Unix timestamp.

范例

示例 #1 A DirectoryIterator::getATime example

Displays a list of the files in the directory of the script and their last access times.

<?php
$iterator = new DirectoryIterator(dirname(__FILE__));
foreach ($iterator as $fileinfo) {
    if ($fileinfo->isFile()) {
        echo $fileinfo->getFilename() . " " . $fileinfo->getATime() . "\n";
    }
}
?>

以上例程的输出类似于:

apple.jpg 1240047118
banana.jpg 1240065176
index.php 1240047208
pear.jpg 12240047979

参见

  • DirectoryIterator::getCTime
  • DirectoryIterator::getMTime
  • fileatime

DirectoryIterator::getBasename

Get base name of current DirectoryIterator item

说明

public string DirectoryIterator::getBasename ([ string $suffix ] )

Get the base name of the current DirectoryIterator item.

参数

suffix
If the base name ends in suffix, this will be cut.

返回值

The base name of the current DirectoryIterator item.

范例

示例 #1 A DirectoryIterator::getBasename example

This example will list the full base name and the base name with suffix .jpg removed for the files in the directory containing the script.

<?php
$dir = new DirectoryIterator(dirname(__FILE__));
foreach ($dir as $fileinfo) {
    if ($fileinfo->isFile()) {
        echo $fileinfo->getBasename() . "\n";
        echo $fileinfo->getBasename('.jpg') . "\n";
    }
}
?>

以上例程的输出类似于:

apple.jpg
apple
banana.jpg
banana
index.php
index.php
pear.jpg
pear

参见

  • DirectoryIterator::getFilename
  • DirectoryIterator::getPath
  • DirectoryIterator::getPathname
  • basename
  • pathinfo

DirectoryIterator::getCTime

Get inode change time of the current DirectoryIterator item

说明

public int DirectoryIterator::getCTime ( void )

Get the inode change time for the current DirectoryIterator item.

参数

此函数没有参数。

返回值

Returns the last change time of the file, as a Unix timestamp.

范例

示例 #1 DirectoryIterator::getCTime example

This example displays the file name and last change time of the files in the directory containing the script.

<?php
$iterator = new DirectoryIterator(dirname(__FILE__));
foreach ($iterator as $fileinfo) {
    if ($fileinfo->isFile()) {
        echo $fileinfo->getFilename() . " changed at " . $fileinfo->getCTime() . "\n";
    }
}
?>

以上例程的输出类似于:

apple.jpg changed at 1240398312
banana.jpg changed at 1238605440
index.php changed at 1240398935
pear.jpg changed at 1237423740

参见

  • DirectoryIterator::getATime
  • DirectoryIterator::getMTime
  • filectime

DirectoryIterator::getExtension

Gets the file extension

说明

public string DirectoryIterator::getExtension ( void )

Retrieves the file extension.

参数

此函数没有参数。

返回值

Returns a string containing the file extension, or an empty string if the file has no extension.

范例

示例 #1 DirectoryIterator::getExtension example

<?php

$directory = new DirectoryIterator(__DIR__);
foreach ($directory as $fileinfo) {
    if ($fileinfo->isFile()) {
        echo $fileinfo->getExtension() . "\n";
    }
}

?>

以上例程的输出类似于:

php
txt
jpg
gz

注释

Note:

This method is only available as of PHP 5.3.6. Another way of getting the extension is to use the pathinfo function.

<?php
$extension = pathinfo($fileinfo->getFilename(), PATHINFO_EXTENSION);
?>

参见

  • DirectoryIterator::getFilename
  • DirectoryIterator::getBasename
  • pathinfo

DirectoryIterator::getFilename

Return file name of current DirectoryIterator item

说明

public string DirectoryIterator::getFilename ( void )

Get the file name of the current DirectoryIterator item.

参数

此函数没有参数。

返回值

Returns the file name of the current DirectoryIterator item.

范例

示例 #1 A DirectoryIterator::getFilename example

This example will list the contents of the directory containing the script.

<?php
$dir = new DirectoryIterator(dirname(__FILE__));
foreach ($dir as $fileinfo) {
    echo $fileinfo->getFilename() . "\n";
}
?>

以上例程的输出类似于:

.
..
apple.jpg
banana.jpg
index.php
pear.jpg

参见

  • DirectoryIterator::getBasename
  • DirectoryIterator::getPath
  • DirectoryIterator::getPathname
  • pathinfo

DirectoryIterator::getGroup

Get group for the current DirectoryIterator item

说明

public int DirectoryIterator::getGroup ( void )

Get the group id of the file.

参数

此函数没有参数。

返回值

Returns the group id of the current DirectoryIterator item in numerical format.

范例

示例 #1 DirectoryIterator::getGroup example

<?php
$iterator = new DirectoryIterator(dirname(__FILE__));
$groupid  = $iterator->getGroup();
echo 'Directory belongs to group id ' . $groupid . "\n";
print_r(posix_getgrgid($groupid));
?>

以上例程的输出类似于:

Directory belongs to group id 42
Array
(
    [name]    => toons
    [passwd]  => x
    [members] => Array
        (
            [0] => tom
            [1] => jerry
        )
    [gid]     => 42
)

参见

  • DirectoryIterator::getiNode
  • DirectoryIterator::getOwner
  • DirectoryIterator::getPerms
  • filegroup
  • posix_getgrgid

DirectoryIterator::getInode

Get inode for the current DirectoryIterator item

说明

public int DirectoryIterator::getInode ( void )

Get the inode number for the current DirectoryIterator item.

参数

此函数没有参数。

返回值

Returns the inode number for the file.

范例

示例 #1 DirectoryIterator::getInode example

This example displays the inode number for the directory containing the script.

<?php
$iterator = new DirectoryIterator(dirname(__FILE__));
echo $iterator->getInode();
?>

参见

  • DirectoryIterator::getGroup
  • DirectoryIterator::getOwner
  • DirectoryIterator::getPerms
  • fileinode

DirectoryIterator::getMTime

Get last modification time of current DirectoryIterator item

说明

public int DirectoryIterator::getMTime ( void )

Get the last modification time of the current DirectoryIterator item, as a Unix timestamp.

参数

此函数没有参数。

返回值

The last modification time of the file, as a Unix timestamp.

范例

示例 #1 A DirectoryIterator::getMTime example

Displays a list of the files in the directory of the script and their last modified times.

<?php
$iterator = new DirectoryIterator(dirname(__FILE__));
foreach ($iterator as $fileinfo) {
    if ($fileinfo->isFile()) {
        echo $fileinfo->getFilename() . " " . $fileinfo->getMTime() . "\n";
    }
}
?>

以上例程的输出类似于:

apple.jpg 1240047118
banana.jpg 1240065176
index.php 1240047208
pear.jpg 12240047979

参见

  • DirectoryIterator::getATime
  • DirectoryIterator::getCTime
  • filemtime

DirectoryIterator::getOwner

Get owner of current DirectoryIterator item

说明

public int DirectoryIterator::getOwner ( void )

Get the owner of the current DirectoryIterator item, in numerical format.

参数

此函数没有参数。

返回值

The file owner of the file, in numerical format.

范例

示例 #1 DirectoryIterator::getOwner example

This example displays the owner of the directory which contains the script.

<?php
$iterator = new DirectoryIterator(dirname(__FILE__));
print_r(posix_getpwuid($iterator->getOwner()));
?>

以上例程的输出类似于:

Array
(
    [name] => tom
    [passwd] => x
    [uid] => 501
    [gid] => 42
    [gecos] => Tom Cat
    [dir] => /home/tom
    [shell] => /bin/bash
)

参见

  • DirectoryIterator::getGroup
  • DirectoryIterator::getiNode
  • DirectoryIterator::getPerms
  • posix_getpwuid

DirectoryIterator::getPath

Get path of current Iterator item without filename

说明

public string DirectoryIterator::getPath ( void )

Get the path to the current DirectoryIterator item.

参数

此函数没有参数。

返回值

Returns the path to the file, omitting the file name and any trailing slash.

范例

示例 #1 DirectoryIterator::getPath example

<?php
$iterator = new DirectoryIterator(dirname(__FILE__));
echo $iterator->getPath();
?>

以上例程的输出类似于:

/home/examples/public_html

参见

  • DirectoryIterator::getBasename
  • DirectoryIterator::getFilename
  • DirectoryIterator::getPathname
  • pathinfo

DirectoryIterator::getPathname

Return path and file name of current DirectoryIterator item

说明

public string DirectoryIterator::getPathname ( void )

Get the path and file name of the current file.

参数

此函数没有参数。

返回值

Returns the path and file name of current file. Directories do not have a trailing slash.

范例

示例 #1 DirectoryIterator::getPathname example

<?php
$iterator = new DirectoryIterator(dirname(__FILE__));
foreach ($iterator as $fileinfo) {
    echo $fileinfo->getPathname() . "\n";
}
?>

以上例程的输出类似于:

/home/examples/.
/home/examples/..
/home/examples/apple.jpg
/home/examples/banana.jpg
/home/examples/getpathname.php
/home/examples/pear.jpg

参见

  • DirectoryIterator::getBasename
  • DirectoryIterator::getFilename
  • DirectoryIterator::getPath
  • pathinfo

DirectoryIterator::getPerms

Get the permissions of current DirectoryIterator item

说明

public int DirectoryIterator::getPerms ( void )

Get the permissions of the current DirectoryIterator item.

参数

此函数没有参数。

返回值

Returns the permissions of the file, as a decimal integer.

范例

示例 #1 DirectoryIterator::getPerms example

<?php
$iterator = new DirectoryIterator(dirname(__FILE__));
foreach ($iterator as $fileinfo) {
    if (!$fileinfo->isDot()) {
        $octal_perms = substr(sprintf('%o', $fileinfo->getPerms()), -4);
        echo $fileinfo->getFilename() . " " . $octal_perms . "\n";
    }
}
?>

以上例程的输出类似于:

apple.jpg 0644
banana.jpg 0644
index.php 0744
pear.jpg 0644

参见

  • DirectoryIterator::isExecutable
  • DirectoryIterator::isReadable
  • DirectoryIterator::isWritable

DirectoryIterator::getSize

Get size of current DirectoryIterator item

说明

public int DirectoryIterator::getSize ( void )

Get the file size for the current DirectoryIterator item.

参数

此函数没有参数。

返回值

Returns the size of the file, in bytes.

范例

示例 #1 DirectoryIterator::getSize example

<?php
$iterator = new DirectoryIterator(dirname(__FILE__));
foreach ($iterator as $fileinfo) {
    if ($fileinfo->isFile()) {
        echo $fileinfo->getFilename() . " " . $fileinfo->getSize() . "\n";
    }
}
?>

以上例程的输出类似于:

apple.jpg 15385
banana.jpg 15190
example.php 170
pear.jpg 34406

参见

  • filesize

DirectoryIterator::getType

Determine the type of the current DirectoryIterator item

说明

public string DirectoryIterator::getType ( void )

Determines which file type the current DirectoryIterator item belongs to. One of file, link, or dir.

参数

此函数没有参数。

返回值

Returns a string representing the type of the file. May be one of file, link, or dir.

范例

示例 #1 DirectoryIterator::getType example

<?php
$iterator = new DirectoryIterator(dirname(__FILE__));
foreach ($iterator as $fileinfo) {
    echo $fileinfo->getFilename() . " " . $fileinfo->getType() . "\n";
}
?>

以上例程的输出类似于:

. dir
.. dir
apple.jpg file
banana.jpg file
example.php file
pear.jpg file

参见

  • DirectoryIterator::isDir
  • DirectoryIterator::isDot
  • DirectoryIterator::isFile
  • DirectoryIterator::isLink

DirectoryIterator::isDir

Determine if current DirectoryIterator item is a directory

说明

public bool DirectoryIterator::isDir ( void )

Determines if the current DirectoryIterator item is a directory.

参数

此函数没有参数。

返回值

Returns TRUE if it is a directory, otherwise FALSE

范例

示例 #1 DirectoryIterator::isDir example

This example lists the directories within the directory of the current script.

<?php
$iterator = new DirectoryIterator(dirname(__FILE__));
foreach ($iterator as $fileinfo) {
    if ($fileinfo->isDir()) {
        echo $fileinfo->getFilename() . "\n";
    }
}
?>

以上例程的输出类似于:

.
..
apples
bananas
pears

参见

  • DirectoryIterator::getType
  • DirectoryIterator::isDot
  • DirectoryIterator::isFile
  • DirectoryIterator::isLink

DirectoryIterator::isDot

Determine if current DirectoryIterator item is '.' or '..'

说明

public bool DirectoryIterator::isDot ( void )

Determines if the current DirectoryIterator item is a directory and either . or ..

参数

此函数没有参数。

返回值

TRUE if the entry is . or .., otherwise FALSE

范例

示例 #1 A DirectoryIterator::isDot example

This example will list all files, omitting the . and .. entries.

<?php
$iterator = new DirectoryIterator(dirname(__FILE__));
foreach ($iterator as $fileinfo) {
    if (!$fileinfo->isDot()) {
        echo $fileinfo->getFilename() . "\n";
    }
}
?>

以上例程的输出类似于:

apple.jpg
banana.jpg
example.php
pears.jpg

参见

  • DirectoryIterator::getType
  • DirectoryIterator::isDir
  • DirectoryIterator::isFile
  • DirectoryIterator::isLink

DirectoryIterator::isExecutable

Determine if current DirectoryIterator item is executable

说明

public bool DirectoryIterator::isExecutable ( void )

Determines if the current DirectoryIterator item is executable.

参数

此函数没有参数。

返回值

Returns TRUE if the entry is executable, otherwise FALSE

范例

示例 #1 DirectoryIterator::isExecutable example

This example lists files in the directory containing the script which are executable.

<?php
$iterator = new DirectoryIterator(dirname(__FILE__));
foreach ($iterator as $fileinfo) {
    if ($fileinfo->isExecutable()) {
        echo $fileinfo->getFilename() . "\n";
    }
}
?>

以上例程的输出类似于:

example.php
myscript.sh

参见

  • DirectoryIterator::isReadable
  • DirectoryIterator::isWritable
  • DirectoryIterator::getPerms

DirectoryIterator::isFile

Determine if current DirectoryIterator item is a regular file

说明

public bool DirectoryIterator::isFile ( void )

Determines if the current DirectoryIterator item is a regular file.

参数

此函数没有参数。

返回值

Returns TRUE if the file exists and is a regular file (not a link or dir), otherwise FALSE

范例

示例 #1 DirectoryIterator::isFile example

This example will list all regular files in the directory containing the script.

<?php
$iterator = new DirectoryIterator(dirname(__FILE__));
foreach ($iterator as $fileinfo) {
    if ($fileinfo->isFile()) {
        echo $fileinfo->getFilename() . "\n";
    }
}
?>

以上例程的输出类似于:

apple.jpg
banana.jpg
example.php
pears.jpg

参见

  • DirectoryIterator::getType
  • DirectoryIterator::isDir
  • DirectoryIterator::isDot
  • DirectoryIterator::isLink

DirectoryIterator::isLink

Determine if current DirectoryIterator item is a symbolic link

说明

public bool DirectoryIterator::isLink ( void )

Determines if the current DirectoryIterator item is a symbolic link.

参数

此函数没有参数。

返回值

Returns TRUE if the item is a symbolic link, otherwise FALSE

范例

示例 #1 A DirectoryIterator::isLink example

This example contains a recursive function for removing a directory tree.

<?php
/**
 * This function will recursively delete all files in the given path, without
 * following symlinks.
 * 
 * @param string $path Path to the directory to remove.
 */
function removeDir($path) {
    $dir = new DirectoryIterator($path);
    foreach ($dir as $fileinfo) {
        if ($fileinfo->isFile() || $fileinfo->isLink()) {
            unlink($fileinfo->getPathName());
        } elseif (!$fileinfo->isDot() && $fileinfo->isDir()) {
            removeDir($fileinfo->getPathName());
        }
    }
    rmdir($path);
}

removeDir('foo');
?>

参见

  • DirectoryIterator::getType
  • DirectoryIterator::isDir
  • DirectoryIterator::isDot
  • DirectoryIterator::isFile

DirectoryIterator::isReadable

Determine if current DirectoryIterator item can be read

说明

public bool DirectoryIterator::isReadable ( void )

Determines if the current DirectoryIterator item is readable.

参数

此函数没有参数。

返回值

Returns TRUE if the file is readable, otherwise FALSE

范例

示例 #1 DirectoryIterator::isReadable example

<?php
$iterator = new DirectoryIterator(dirname(__FILE__));
foreach ($iterator as $fileinfo) {
    if ($fileinfo->isReadable()) {
        echo $fileinfo->getFilename() . "\n";
    }
}
?>

以上例程的输出类似于:

apple.jpg
banana.jpg
example.php
pears.jpg

参见

  • DirectoryIterator::isExecutable
  • DirectoryIterator::isWritable
  • DirectoryIterator::getPerms

DirectoryIterator::isWritable

Determine if current DirectoryIterator item can be written to

说明

public bool DirectoryIterator::isWritable ( void )

Determines if the current DirectoryIterator item is writable.

参数

此函数没有参数。

返回值

Returns TRUE if the file/directory is writable, otherwise FALSE

范例

示例 #1 DirectoryIterator::isWritable example

This example lists the files and directories which can be opened for writing in the directory containing the script.

<?php
$iterator = new DirectoryIterator(dirname(__FILE__));
foreach ($iterator as $fileinfo) {
    if ($fileinfo->isWritable()) {
        echo $fileinfo->getFilename() . "\n";
    }
}
?>

以上例程的输出类似于:

apples.txt
bananas.html
pears

参见

  • DirectoryIterator::getPerms
  • DirectoryIterator::isExecutable
  • DirectoryIterator::isReadable

DirectoryIterator::key

Return the key for the current DirectoryIterator item

说明

public string DirectoryIterator::key ( void )

Get the key for the current DirectoryIterator item.

参数

此函数没有参数。

返回值

The key for the current DirectoryIterator item.

范例

示例 #1 A DirectoryIterator::key example

<?php
$dir = new DirectoryIterator(dirname(__FILE__));
foreach ($dir as $fileinfo) {
    if (!$fileinfo->isDot()) {
        echo $fileinfo->key() . " => " . $fileinfo->getFilename() . "\n";
    }
}
?>

以上例程的输出类似于:

0 => apple.jpg
1 => banana.jpg
2 => index.php
3 => pear.jpg

参见

  • DirectoryIterator::current
  • DirectoryIterator::next
  • DirectoryIterator::rewind
  • DirectoryIterator::valid
  • Iterator::key

DirectoryIterator::next

Move forward to next DirectoryIterator item

说明

public void DirectoryIterator::next ( void )

Move forward to the next DirectoryIterator item.

参数

此函数没有参数。

返回值

没有返回值。

范例

示例 #1 DirectoryIterator::next example

List the contents of a directory using a while loop.

<?php
$iterator = new DirectoryIterator(dirname(__FILE__));
while($iterator->valid()) {
    echo $iterator->getFilename() . "\n";
    $iterator->next();
}
?>

以上例程的输出类似于:

.
..
apple.jpg
banana.jpg
index.php
pear.jpg

参见

  • DirectoryIterator::current
  • DirectoryIterator::key
  • DirectoryIterator::rewind
  • DirectoryIterator::valid
  • Iterator::next

DirectoryIterator::rewind

Rewind the DirectoryIterator back to the start

说明

public void DirectoryIterator::rewind ( void )

Rewind the DirectoryIterator back to the start.

参数

此函数没有参数。

返回值

没有返回值。

范例

示例 #1 DirectoryIterator::rewind example

<?php
$iterator = new DirectoryIterator(dirname(__FILE__));

$iterator->next();
echo $iterator->key(); //1

$iterator->rewind(); //rewinding to the beginning
echo $iterator->key(); //0
?>

参见

  • DirectoryIterator::current
  • DirectoryIterator::key
  • DirectoryIterator::next
  • DirectoryIterator::valid
  • Iterator::rewind

DirectoryIterator::seek

Seek to a DirectoryIterator item

说明

public void DirectoryIterator::seek ( int $position )

Seek to a given position in the DirectoryIterator.

参数

position
The zero-based numeric position to seek to.

返回值

没有返回值。

范例

示例 #1 DirectoryIterator::seek example

Seek to the fourth item in the directory containing the script. The first two are usually . and ..

<?php
$iterator = new DirectoryIterator(dirname(__FILE__));
$iterator->seek(3);
if ($iterator->valid()) {
    echo $iterator->getFilename();
} else {
    echo 'No file at position 3';
}
?>

参见

  • DirectoryIterator::rewind
  • DirectoryIterator::next
  • SeekableIterator::seek

DirectoryIterator::__toString

Get file name as a string

说明

public string DirectoryIterator::__toString ( void )

Get the file name of the current DirectoryIterator item.

参数

此函数没有参数。

返回值

Returns the file name of the current DirectoryIterator item.

范例

示例 #1 A DirectoryIterator::__toString example

This example will list the contents of the directory containing the script.

<?php
$dir = new DirectoryIterator(dirname(__FILE__));
foreach ($dir as $fileinfo) {
    echo $fileinfo;
}
?>

以上例程的输出类似于:

.
..
apple.jpg
banana.jpg
index.php
pear.jpg

参见

  • DirectoryIterator::getFilename
  • The __toString() magic method

DirectoryIterator::valid

Check whether current DirectoryIterator position is a valid file

说明

public bool DirectoryIterator::valid ( void )

Check whether current DirectoryIterator position is a valid file.

参数

此函数没有参数。

返回值

Returns TRUE if the position is valid, otherwise FALSE

范例

示例 #1 A DirectoryIterator::valid example

<?php
$iterator = new DirectoryIterator(dirname(__FILE__));

// Loop to end of iterator
while($iterator->valid()) {
    $iterator->next();
}

$iterator->valid(); // FALSE
$iterator->rewind(); 
$iterator->valid(); // TRUE

?>

参见

  • DirectoryIterator::current
  • DirectoryIterator::key
  • DirectoryIterator::next
  • DirectoryIterator::rewind
  • Iterator::valid

简介

The EmptyIterator class for an empty iterator.

类摘要

EmptyIterator

class EmptyIterator implements Iterator {

/* 方法 */

public mixed current ( void )

public scalar key ( void )

public void next ( void )

public void rewind ( void )

public bool valid ( void )

}

EmptyIterator::current

The current() method

说明

public mixed EmptyIterator::current ( void )

This function must not be called. It throws an exception upon access.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

错误/异常

Throws an Exception if called.

返回值

没有返回值。

EmptyIterator::key

The key() method

说明

public scalar EmptyIterator::key ( void )

This function must not be called. It throws an exception upon access.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

错误/异常

Throws an Exception if called.

返回值

没有返回值。

EmptyIterator::next

The next() method

说明

public void EmptyIterator::next ( void )

No operation, nothing to do.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

没有返回值。

EmptyIterator::rewind

The rewind() method

说明

public void EmptyIterator::rewind ( void )

No operation, nothing to do.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

没有返回值。

EmptyIterator::valid

The valid() method

说明

public bool EmptyIterator::valid ( void )

The EmptyIterator valid() method.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

FALSE

简介

The Filesystem iterator

类摘要

FilesystemIterator

class FilesystemIterator extends DirectoryIterator implements SeekableIterator {

/* 常量 */

const integer FilesystemIterator::CURRENT_AS_PATHNAME = 32 ;

const integer FilesystemIterator::CURRENT_AS_FILEINFO = 0 ;

const integer FilesystemIterator::CURRENT_AS_SELF = 16 ;

const integer FilesystemIterator::CURRENT_MODE_MASK = 240 ;

const integer FilesystemIterator::KEY_AS_PATHNAME = 0 ;

const integer FilesystemIterator::KEY_AS_FILENAME = 256 ;

const integer FilesystemIterator::FOLLOW_SYMLINKS = 512 ;

const integer FilesystemIterator::KEY_MODE_MASK = 3840 ;

const integer FilesystemIterator::NEW_CURRENT_AND_KEY = 256 ;

const integer FilesystemIterator::SKIP_DOTS = 4096 ;

const integer FilesystemIterator::UNIX_PATHS = 8192 ;

/* 方法 */

public __construct ( string $path [, int $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS ] )

public mixed current ( void )

public int getFlags ( void )

public string key ( void )

public void next ( void )

public void rewind ( void )

public void setFlags ([ int $flags ] )

/* 继承的方法 */

public DirectoryIterator DirectoryIterator::current ( void )

public int DirectoryIterator::getATime ( void )

public string DirectoryIterator::getBasename ([ string $suffix ] )

public int DirectoryIterator::getCTime ( void )

public string DirectoryIterator::getExtension ( void )

public string DirectoryIterator::getFilename ( void )

public int DirectoryIterator::getGroup ( void )

public int DirectoryIterator::getInode ( void )

public int DirectoryIterator::getMTime ( void )

public int DirectoryIterator::getOwner ( void )

public string DirectoryIterator::getPath ( void )

public string DirectoryIterator::getPathname ( void )

public int DirectoryIterator::getPerms ( void )

public int DirectoryIterator::getSize ( void )

public string DirectoryIterator::getType ( void )

public bool DirectoryIterator::isDir ( void )

public bool DirectoryIterator::isDot ( void )

public bool DirectoryIterator::isExecutable ( void )

public bool DirectoryIterator::isFile ( void )

public bool DirectoryIterator::isLink ( void )

public bool DirectoryIterator::isReadable ( void )

public bool DirectoryIterator::isWritable ( void )

public string DirectoryIterator::key ( void )

public void DirectoryIterator::next ( void )

public void DirectoryIterator::rewind ( void )

public void DirectoryIterator::seek ( int $position )

public string DirectoryIterator::__toString ( void )

public bool DirectoryIterator::valid ( void )

}

预定义常量

FilesystemIterator::CURRENT_AS_PATHNAME
Makes FilesystemIterator::current return the pathname.

FilesystemIterator::CURRENT_AS_FILEINFO
Makes FilesystemIterator::current return an SplFileInfo instance.

FilesystemIterator::CURRENT_AS_SELF
Makes FilesystemIterator::current return $this (the FilesystemIterator).

FilesystemIterator::CURRENT_MODE_MASK
Masks FilesystemIterator::current

FilesystemIterator::KEY_AS_PATHNAME
Makes FilesystemIterator::key return the pathname.

FilesystemIterator::KEY_AS_FILENAME
Makes FilesystemIterator::key return the filename.

FilesystemIterator::FOLLOW_SYMLINKS
Makes RecursiveDirectoryIterator::hasChildren follow symlinks.

FilesystemIterator::KEY_MODE_MASK
Masks FilesystemIterator::key

FilesystemIterator::NEW_CURRENT_AND_KEY
Same as FilesystemIterator::KEY_AS_FILENAME | FilesystemIterator::CURRENT_AS_FILEINFO.

FilesystemIterator::SKIP_DOTS
Skips dot files (. and ..).

FilesystemIterator::UNIX_PATHS
Makes paths use Unix-style forward slash irrespective of system default. Note that the path that is passed to the constructor is not modified.

更新日志

版本说明
5.3.1Added FilesystemIterator::FOLLOW_SYMLINKS

FilesystemIterator::__construct

Constructs a new filesystem iterator

说明

public FilesystemIterator::__construct ( string $path [, int $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS ] )

Constructs a new filesystem iterator from the path.

参数

path
The path of the filesystem item to be iterated over.

flags
Flags may be provided which will affect the behavior of some methods. A list of the flags can found under FilesystemIterator predefined constants. They can also be set later with FilesystemIterator::setFlags

返回值

没有返回值。

错误/异常

Throws an UnexpectedValueException if the path cannot be found.

范例

示例 #1 FilesystemIterator::__construct example

<?php
$it = new FilesystemIterator(dirname(__FILE__));
foreach ($it as $fileinfo) {
    echo $fileinfo->getFilename() . "\n";
}
?>

以上例程会输出:

apples.jpg
banana.jpg
example.php

参见

  • FilesystemIterator::setFlags
  • DirectoryIterator::__construct

FilesystemIterator::current

The current file

说明

public mixed FilesystemIterator::current ( void )

Get file information of the current element.

参数

此函数没有参数。

返回值

The filename, file information, or $this depending on the set flags. See the FilesystemIterator constants.

范例

示例 #1 FilesystemIterator::current example

This example will list the contents of the directory containing the script.

<?php
$iterator = new FilesystemIterator(__DIR__, FilesystemIterator::CURRENT_AS_PATHNAME);
foreach ($iterator as $fileinfo) {
    echo $iterator->current() . "\n";
}
?>

以上例程的输出类似于:

/www/examples/apple.jpg
/www/examples/banana.jpg
/www/examples/example.php

参见

FilesystemIterator::getFlags

Get the handling flags

说明

public int FilesystemIterator::getFlags ( void )

Gets the handling flags, as set in FilesystemIterator::__construct or FilesystemIterator::setFlags.

参数

此函数没有参数。

返回值

The integer value of the set flags.

参见

  • FilesystemIterator::__construct
  • FilesystemIterator::setFlags

FilesystemIterator::key

Retrieve the key for the current file

说明

public string FilesystemIterator::key ( void )

参数

此函数没有参数。

返回值

Returns the pathname or filename depending on the set flags. See the FilesystemIterator constants.

范例

示例 #1 FilesystemIterator::key example

This example will list the contents of the directory containing the script.

<?php
$iterator = new FilesystemIterator(dirname(__FILE__), FilesystemIterator::KEY_AS_FILENAME);
foreach ($iterator as $fileinfo) {
    echo $iterator->key() . "\n";
}
?>

以上例程的输出类似于:

apple.jpg
banana.jpg
example.php

参见

FilesystemIterator::next

Move to the next file

说明

public void FilesystemIterator::next ( void )

Move to the next file.

参数

此函数没有参数。

返回值

没有返回值。

范例

示例 #1 FilesystemIterator::next example

List the contents of a directory using a while loop.

<?php
$iterator = new FilesystemIterator(dirname(__FILE__));
while($iterator->valid()) {
    echo $iterator->getFilename() . "\n";
    $iterator->next();
}
?>

以上例程的输出类似于:

apple.jpg
banana.jpg
example.php

参见

  • DirectoryIterator::next

FilesystemIterator::rewind

Rewinds back to the beginning

说明

public void FilesystemIterator::rewind ( void )

Rewinds the directory back to the start.

参数

此函数没有参数。

返回值

没有返回值。

范例

示例 #1 FilesystemIterator::rewind example

<?php
$iterator = new FilesystemIterator(dirname(__FILE__), FilesystemIterator::KEY_AS_FILENAME);

echo $iterator->key() . "\n";

$iterator->next();
echo $iterator->key() . "\n";

$iterator->rewind();
echo $iterator->key() . "\n";
?>

以上例程的输出类似于:

apple.jpg
banana.jpg
apple.jpg

参见

  • DirectoryIterator::rewind

FilesystemIterator::setFlags

Sets handling flags

说明

public void FilesystemIterator::setFlags ([ int $flags ] )

Sets handling flags.

参数

flags
The handling flags to set. See the FilesystemIterator constants.

返回值

没有返回值。

范例

示例 #1 FilesystemIterator::key example

This example demonstrates the difference between the FilesystemIterator::KEY_AS_PATHNAME and FilesystemIterator::KEY_AS_FILENAME flags.

<?php
$iterator = new FilesystemIterator(dirname(__FILE__), FilesystemIterator::KEY_AS_PATHNAME);
echo "Key as Pathname:\n";
foreach ($iterator as $key => $fileinfo) {
    echo $key . "\n";
}

$iterator->setFlags(FilesystemIterator::KEY_AS_FILENAME);
echo "\nKey as Filename:\n";
foreach ($iterator as $key => $fileinfo) {
    echo $key . "\n";
}
?>

以上例程的输出类似于:

Key as Pathname:
/www/examples/apple.jpg
/www/examples/banana.jpg
/www/examples/example.php

Key as Filename:
apple.jpg
banana.jpg
example.php

参见

  • FilesystemIterator::__construct
  • FilesystemIterator::getFlags

简介

这个抽象类的遍历并过滤出不想要的值.这个类应该被实现了迭代过滤器的类继承 FilterIterator::accept方法必须被子类实现.

类摘要

FilterIterator

abstract class FilterIterator extends IteratorIterator implements OuterIterator {

/* 方法 */

public abstract bool accept ( void )

public __construct ( Iterator $iterator )

public mixed current ( void )

public Iterator getInnerIterator ( void )

public mixed key ( void )

public void next ( void )

public void rewind ( void )

public bool valid ( void )

}

FilterIterator::accept

Check whether the current element of the iterator is acceptable

说明

public abstract bool FilterIterator::accept ( void )

Returns whether the current element of the iterator is acceptable through this filter.

参数

此函数没有参数。

返回值

TRUE if the current element is acceptable, otherwise FALSE.

范例

示例 #1 FilterIterator::accept example

<?php
// This iterator filters all values with less than 10 characters
class LengthFilterIterator extends FilterIterator {

    public function accept() {
        // Only accept strings with a length of 10 and greater
        return strlen(parent::current()) >= 10;
    }

}

$arrayIterator = new ArrayIterator(array('test1', 'more than 10 characters'));
$lengthFilter = new LengthFilterIterator($arrayIterator);

foreach ($lengthFilter as $value) {
    echo $value . "\n";
}
?>

以上例程会输出:

more than 10 characters

FilterIterator::__construct

Construct a filterIterator

说明

public FilterIterator::__construct ( Iterator $iterator )

Constructs a new FilterIterator, which consists of a passed in iterator with filters applied to it.

Warning

本函数还未编写文档,仅有参数列表。

参数

iterator
The iterator that is being filtered.

返回值

The FilterIterator.

参见

  • LimitIterator::__construct

FilterIterator::current

Get the current element value

说明

public mixed FilterIterator::current ( void )

Warning

本函数还未编写文档,仅有参数列表。

Get the current element value.

参数

此函数没有参数。

返回值

The current element value.

参见

  • FilterIterator::key
  • FilterIterator::next

FilterIterator::getInnerIterator

Get the inner iterator

说明

public Iterator FilterIterator::getInnerIterator ( void )

Warning

本函数还未编写文档,仅有参数列表。

Get the inner iterator.

参数

此函数没有参数。

返回值

The inner iterator.

FilterIterator::key

Get the current key

说明

public mixed FilterIterator::key ( void )

Warning

本函数还未编写文档,仅有参数列表。

Get the current key.

参数

此函数没有参数。

返回值

The current key.

参见

  • FilterIterator::next
  • FilterIterator::current

FilterIterator::next

Move the iterator forward

说明

public void FilterIterator::next ( void )

Warning

本函数还未编写文档,仅有参数列表。

Move the iterator forward.

参数

此函数没有参数。

返回值

没有返回值。

参见

  • FilterIterator::current
  • FilterIterator::key

FilterIterator::rewind

Rewind the iterator

说明

public void FilterIterator::rewind ( void )

Warning

本函数还未编写文档,仅有参数列表。

Rewind the iterator.

参数

此函数没有参数。

返回值

没有返回值。

参见

  • FilterIterator::current
  • FilterIterator::key
  • FilterIterator::next

FilterIterator::valid

Check whether the current element is valid

说明

public bool FilterIterator::valid ( void )

Warning

本函数还未编写文档,仅有参数列表。

Checks whether the current element is valid.

参数

此函数没有参数。

返回值

TRUE if the current element is valid, otherwise FALSE

简介

遍历一个文件系统行为类似于 glob.

类摘要

GlobIterator

class GlobIterator extends FilesystemIterator implements SeekableIterator , Countable {

/* 方法 */

public __construct ( string $pattern [, int $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO ] )

public int count ( void )

/* 继承的方法 */

public FilesystemIterator::__construct ( string $path [, int $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS ] )

public mixed FilesystemIterator::current ( void )

public int FilesystemIterator::getFlags ( void )

public string FilesystemIterator::key ( void )

public void FilesystemIterator::next ( void )

public void FilesystemIterator::rewind ( void )

public void FilesystemIterator::setFlags ([ int $flags ] )

}

GlobIterator::__construct

Construct a directory using glob

说明

public GlobIterator::__construct ( string $pattern [, int $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO ] )

Constructs a new directory iterator from a glob expression.

参数

pattern
A glob pattern.

flags
Option flags, the flags may be a bitmask of the FilesystemIterator constants.

范例

示例 #1 GlobIterator example

<?php
$iterator = new GlobIterator('*.dll', FilesystemIterator::KEY_AS_FILENAME);

if (!$iterator->count()) {
    echo 'No matches';
} else {
    $n = 0;

    printf("Matched %d item(s)\r\n", $iterator->count());

    foreach ($iterator as $item) {
        printf("[%d] %s\r\n", ++$n, $iterator->key());
    }
}
?>

以上例程的输出类似于:

Matched 2 item(s)
[1] php5ts.dll
[2] php_gd2.dll

参见

  • DirectoryIterator::__construct
  • GlobIterator::count
  • glob

GlobIterator::count

Get the number of directories and files

说明

public int GlobIterator::count ( void )

Gets the number of directories and files found by the glob expression.

参数

此函数没有参数。

返回值

The number of returned directories and files, as an integer.

范例

示例 #1 GlobIterator::count example

<?php
$iterator = new GlobIterator('*.xml');

printf("Matched %d item(s)\r\n", $iterator->count());
?>

以上例程的输出类似于:

Matched 8 item(s)

参见

  • GlobIterator::__construct
  • count
  • glob

简介

The InfiniteIterator allows one to infinitely iterate over an iterator without having to manually rewind the iterator upon reaching its end.

类摘要

InfiniteIterator

class InfiniteIterator extends IteratorIterator implements OuterIterator {

/* 方法 */

public __construct ( Iterator $iterator )

public void next ( void )

/* 继承的方法 */

public IteratorIterator::__construct ( Traversable $iterator )

public mixed IteratorIterator::current ( void )

public Traversable IteratorIterator::getInnerIterator ( void )

public mixed IteratorIterator::key ( void )

public void IteratorIterator::next ( void )

public void IteratorIterator::rewind ( void )

public bool IteratorIterator::valid ( void )

}

InfiniteIterator::__construct

Constructs an InfiniteIterator

说明

public InfiniteIterator::__construct ( Iterator $iterator )

Constructs an InfiniteIterator from an Iterator.

参数

iterator
The iterator to infinitely iterate over.

返回值

没有返回值。

错误/异常

Throws an E_RECOVERABLE_ERROR if the iterator parameter is not an Iterator.

范例

示例 #1 InfiniteIterator::__construct example

<?php
$arrayit  = new ArrayIterator(array('cat','dog'));
$infinite = new InfiniteIterator($arrayit);
$limit    = new LimitIterator($infinite, 0, 7);
foreach($limit as $value)
{
    echo "$value\n";
}
?>

以上例程会输出:

cat
dog
cat
dog
cat
dog
cat

参见

  • InfiniteIterator::next

InfiniteIterator::next

Moves the inner Iterator forward or rewinds it

说明

public void InfiniteIterator::next ( void )

Moves the inner Iterator forward to its next element if there is one, otherwise rewinds the inner Iterator back to the beginning.

Note:

Even an InfiniteIterator stops if its inner Iterator is empty.

参数

此函数没有参数。

返回值

没有返回值。

参见

  • InfiniteIterator::__construct

简介

This iterator wrapper allows the conversion of anything that is Traversable into an Iterator. It is important to understand that most classes that do not implement Iterators have reasons as most likely they do not allow the full Iterator feature set. If so, techniques should be provided to prevent misuse, otherwise expect exceptions or fatal errors.

类摘要

IteratorIterator

class IteratorIterator implements OuterIterator {

/* 方法 */

public __construct ( Traversable $iterator )

public mixed current ( void )

public Traversable getInnerIterator ( void )

public mixed key ( void )

public void next ( void )

public void rewind ( void )

public bool valid ( void )

}

注释

Note:

This class permits access to methods of the inner iterator via the __call magic method.

IteratorIterator::__construct

Create an iterator from anything that is traversable

说明

public IteratorIterator::__construct ( Traversable $iterator )

Creates an iterator from anything that is traversable.

参数

iterator
The traversable iterator.

返回值

没有返回值。

参见

  • Traversable

IteratorIterator::current

Get the current value

说明

public mixed IteratorIterator::current ( void )

Get the value of the current element.

参数

此函数没有参数。

返回值

The value of the current element.

参见

  • IteratorIterator::key

IteratorIterator::getInnerIterator

Get the inner iterator

说明

public Traversable IteratorIterator::getInnerIterator ( void )

Get the inner iterator.

参数

此函数没有参数。

返回值

The inner iterator as passed to IteratorIterator::__construct.

参见

  • Iterator
  • OuterIterator

IteratorIterator::key

Get the key of the current element

说明

public mixed IteratorIterator::key ( void )

Get the key of the current element.

参数

此函数没有参数。

返回值

The key of the current element.

参见

  • IteratorIterator::current

IteratorIterator::next

Forward to the next element

说明

public void IteratorIterator::next ( void )

Forward to the next element.

参数

此函数没有参数。

返回值

没有返回值。

参见

  • IteratorIterator::rewind
  • IteratorIterator::valid

IteratorIterator::rewind

Rewind to the first element

说明

public void IteratorIterator::rewind ( void )

Rewinds to the first element.

参数

此函数没有参数。

返回值

没有返回值。

参见

  • IteratorIterator::next
  • IteratorIterator::valid

IteratorIterator::valid

Checks if the iterator is valid

说明

public bool IteratorIterator::valid ( void )

Checks if the iterator is valid.

参数

此函数没有参数。

返回值

Returns TRUE if the iterator is valid, otherwise FALSE

参见

  • iterator_count
  • IteratorIterator::current

简介

LimitIterator类允许遍历一个 Iterator 的限定子集的元素.

类摘要

LimitIterator

class LimitIterator extends IteratorIterator implements OuterIterator {

/* 方法 */

public __construct ( Iterator $iterator [, int $offset = 0 [, int $count = -1 ]] )

public mixed current ( void )

public Iterator getInnerIterator ( void )

public int getPosition ( void )

public mixed key ( void )

public void next ( void )

public void rewind ( void )

public int seek ( int $position )

public bool valid ( void )

}

范例

示例 #1 LimitIterator usage example

<?php

// Create an iterator to be limited
$fruits = new ArrayIterator(array(
    'apple',
    'banana',
    'cherry',
    'damson',
    'elderberry'
));

// Loop over first three fruits only
foreach (new LimitIterator($fruits, 0, 3) as $fruit) {
    var_dump($fruit);
}

echo "\n";

// Loop from third fruit until the end
// Note: offset starts from zero for apple
foreach (new LimitIterator($fruits, 2) as $fruit) {
    var_dump($fruit);
}

?>

以上例程会输出:

string(5) "apple"
string(6) "banana"
string(6) "cherry"

string(6) "cherry"
string(6) "damson"
string(10) "elderberry"

LimitIterator::__construct

Construct a LimitIterator

说明

public LimitIterator::__construct ( Iterator $iterator [, int $offset = 0 [, int $count = -1 ]] )

Constructs a new LimitIterator from an iterator with a given starting offset and maximum count.

参数

iterator
The Iterator to limit.

offset
Optional offset of the limit.

count
Optional count of the limit.

返回值

The new LimitIterator.

错误/异常

Throws an OutOfRangeException if the offset is less than 0 or the count is less than -1.

范例

示例 #1 LimitIterator::__construct example

<?php
$ait = new ArrayIterator(array('a', 'b', 'c', 'd', 'e'));
$lit = new LimitIterator($ait, 1, 3);
foreach ($lit as $value) {
    echo $value . "\n";
}
?>

以上例程会输出:

b
c
d

参见

LimitIterator::current

Get current element

说明

public mixed LimitIterator::current ( void )

Gets the current element of the inner Iterator.

参数

此函数没有参数。

返回值

Returns the current element or NULL if there is none.

参见

  • LimitIterator::key
  • LimitIterator::next
  • LimitIterator::rewind
  • LimitIterator::seek
  • LimitIterator::valid

LimitIterator::getInnerIterator

Get inner iterator

说明

public Iterator LimitIterator::getInnerIterator ( void )

Gets the inner Iterator.

参数

此函数没有参数。

返回值

The inner iterator passed to LimitIterator::__construct.

参见

  • LimitIterator::__construct
  • IteratorIterator::getInnerIterator

LimitIterator::getPosition

Return the current position

说明

public int LimitIterator::getPosition ( void )

Gets the current zero-based position of the inner Iterator.

参数

此函数没有参数。

返回值

The current position.

范例

示例 #1 LimitIterator::getPosition example

<?php
$fruits = array(
    'a' => 'apple',
    'b' => 'banana',
    'c' => 'cherry',
    'd' => 'damson',
    'e' => 'elderberry'
);
$array_it = new ArrayIterator($fruits);
$limit_it = new LimitIterator($array_it, 2, 3);
foreach ($limit_it as $item) {
    echo $limit_it->getPosition() . ' ' . $item . "\n";
}
?>

以上例程会输出:

2 cherry
3 damson
4 elderberry

参见

  • FilterIterator::key

LimitIterator::key

Get current key

说明

public mixed LimitIterator::key ( void )

Gets the key for the current item in the inner Iterator.

参数

此函数没有参数。

返回值

Returns the key for the current item.

参见

  • LimitIterator::getPosition
  • LimitIterator::current
  • LimitIterator::next
  • LimitIterator::rewind
  • LimitIterator::seek
  • LimitIterator::valid

LimitIterator::next

Move the iterator forward

说明

public void LimitIterator::next ( void )

Moves the iterator forward.

参数

此函数没有参数。

返回值

没有返回值。

参见

  • LimitIterator::current
  • LimitIterator::key
  • LimitIterator::rewind
  • LimitIterator::seek
  • LimitIterator::valid

LimitIterator::rewind

Rewind the iterator to the specified starting offset

说明

public void LimitIterator::rewind ( void )

Rewinds the iterator to the starting offset specified in LimitIterator::__construct.

参数

此函数没有参数。

返回值

没有返回值。

参见

  • LimitIterator::current
  • LimitIterator::key
  • LimitIterator::next
  • LimitIterator::seek
  • LimitIterator::valid

LimitIterator::seek

Seek to the given position

说明

public int LimitIterator::seek ( int $position )

Moves the iterator to the offset specified by position.

参数

position
The position to seek to.

返回值

Returns the offset position after seeking.

错误/异常

Throws an OutOfBoundsException if the position is outside of the limits specified in LimitIterator::__construct.

参见

  • LimitIterator::current
  • LimitIterator::key
  • LimitIterator::rewind
  • LimitIterator::next
  • LimitIterator::valid

LimitIterator::valid

Check whether the current element is valid

说明

public bool LimitIterator::valid ( void )

Checks whether the current element is valid.

参数

此函数没有参数。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

参见

  • LimitIterator::current
  • LimitIterator::key
  • LimitIterator::rewind
  • LimitIterator::next
  • LimitIterator::seek

简介

An Iterator that sequentially iterates over all attached iterators

类摘要

MultipleIterator

class MultipleIterator implements Iterator {

/* 常量 */

const integer MultipleIterator::MIT_NEED_ANY = 0 ;

const integer MultipleIterator::MIT_NEED_ALL = 1 ;

const integer MultipleIterator::MIT_KEYS_NUMERIC = 0 ;

const integer MultipleIterator::MIT_KEYS_ASSOC = 2 ;

/* 方法 */

public __construct ([ int $flags = MultipleIterator::MIT_NEED_ALL|MultipleIterator::MIT_KEYS_NUMERIC ] )

public void attachIterator ( Iterator $iterator [, string $infos ] )

public bool containsIterator ( Iterator $iterator )

public int countIterators ( void )

public array current ( void )

public void detachIterator ( Iterator $iterator )

public int getFlags ( void )

public array key ( void )

public void next ( void )

public void rewind ( void )

public void setFlags ( int $flags )

public bool valid ( void )

}

预定义常量

MultipleIterator::MIT_NEED_ANY
Do not require all sub iterators to be valid in iteration.

MultipleIterator::MIT_NEED_ALL
Require all sub iterators to be valid in iteration.

MultipleIterator::MIT_KEYS_NUMERIC
Keys are created from the sub iterators position.

MultipleIterator::MIT_KEYS_ASSOC
Keys are created from sub iterators associated information.

MultipleIterator::attachIterator

Attaches iterator information

说明

public void MultipleIterator::attachIterator ( Iterator $iterator [, string $infos ] )

Attaches iterator information.

Warning

本函数还未编写文档,仅有参数列表。

参数

iterator
The new iterator to attach.

infos
The associative information for the Iterator, which must be an integer, a string, or NULL.

返回值

Description...

错误/异常

An IllegalValueException if the iterator parameter is invalid, or if infos is already associated information.

参见

  • MultipleIterator::__construct

MultipleIterator::__construct

Constructs a new MultipleIterator

说明

public MultipleIterator::__construct ([ int $flags = MultipleIterator::MIT_NEED_ALL|MultipleIterator::MIT_KEYS_NUMERIC ] )

Construct a new MultipleIterator.

参数

flags
The flags to set, according to the Flag Constants.

  • MultipleIterator::MIT_NEED_ALL or MultipleIterator::MIT_NEED_ANY
  • MultipleIterator::MIT_KEYS_NUMERIC or MultipleIterator::MIT_KEYS_ASSOC

Defaults to MultipleIterator::MIT_NEED_ALL|MultipleIterator::MIT_KEYS_NUMERIC.

返回值

没有返回值。

范例

示例 #1 Iterating a MultipleIterator

<?php
$people = new ArrayIterator(array('John', 'Jane', 'Jack', 'Judy'));
$roles  = new ArrayIterator(array('Developer', 'Scrum Master', 'Project Owner'));

$team = new MultipleIterator($flags);
$team->attachIterator($people, 'person');
$team->attachIterator($roles, 'role');

foreach ($team as $member) {
    print_r($member);
}
?>

Output with $flags = MIT_NEED_ALL|MIT_KEYS_NUMERIC

Array
(
    [0] => John
    [1] => Developer
)
Array
(
    [0] => Jane
    [1] => Scrum Master
)
Array
(
    [0] => Jack
    [1] => Project Owner
)

Output with $flags = MIT_NEED_ANY|MIT_KEYS_NUMERIC

Array
(
    [0] => John
    [1] => Developer
)
Array
(
    [0] => Jane
    [1] => Scrum Master
)
Array
(
    [0] => Jack
    [1] => Project Owner
)
Array
(
    [0] => Judy
    [1] =>
)

Output with $flags = MIT_NEED_ALL|MIT_KEYS_ASSOC

Array
(
    [person] => John
    [role] => Developer
)
Array
(
    [person] => Jane
    [role] => Scrum Master
)
Array
(
    [person] => Jack
    [role] => Project Owner
)

Output with $flags = MIT_NEED_ANY|MIT_KEYS_ASSOC

Array
(
    [person] => John
    [role] => Developer
)
Array
(
    [person] => Jane
    [role] => Scrum Master
)
Array
(
    [person] => Jack
    [role] => Project Owner
)
Array
(
    [person] => Judy
    [role] =>
)

参见

MultipleIterator::containsIterator

Checks if an iterator is attached

说明

public bool MultipleIterator::containsIterator ( Iterator $iterator )

Checks if an iterator is attached or not.

Warning

本函数还未编写文档,仅有参数列表。

参数

iterator
The iterator to check.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

参见

  • MultipleIterator::valid

MultipleIterator::countIterators

Gets the number of attached iterator instances

说明

public int MultipleIterator::countIterators ( void )

Gets the number of attached iterator instances.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

The number of attached iterator instances (as an integer).

参见

  • MultipleIterator::containsIterator

MultipleIterator::current

Gets the registered iterator instances

说明

public array MultipleIterator::current ( void )

Get the registered iterator instances current() result.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

An array containing the current values of each attached iterator, or FALSE if no iterators are attached.

错误/异常

A RuntimeException if mode MIT_NEED_ALL is set and at least one attached iterator is not valid. Or an IllegalValueException if a key is NULL and MIT_KEYS_ASSOC is set.

参见

  • MultipleIterator::valid

MultipleIterator::detachIterator

Detaches an iterator

说明

public void MultipleIterator::detachIterator ( Iterator $iterator )

Detaches an iterator.

Warning

本函数还未编写文档,仅有参数列表。

参数

iterator
The iterator to detach.

返回值

没有返回值。

参见

  • MultipleIterator::attachIterator

MultipleIterator::getFlags

Gets the flag information

说明

public int MultipleIterator::getFlags ( void )

Gets information about the flags.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

Information about the flags, as an integer.

参见

  • Flag Constants
  • MultipleIterator::__construct
  • MultipleIterator::setFlags

MultipleIterator::key

Gets the registered iterator instances

说明

public array MultipleIterator::key ( void )

Get the registered iterator instances key() result.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

An array of all registered iterator instances, or FALSE if no sub iterator is attached.

错误/异常

A LogicException if mode MIT_NEED_ALL is set, and at least one attached iterator is not valid.

Calling this method from foreach triggers warning "Illegal type returned".

参见

  • MultipleIterator::current

MultipleIterator::next

Moves all attached iterator instances forward

说明

public void MultipleIterator::next ( void )

Moves all attached iterator instances forward.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

没有返回值。

参见

  • MultipleIterator::rewind

MultipleIterator::rewind

Rewinds all attached iterator instances

说明

public void MultipleIterator::rewind ( void )

Rewinds all attached iterator instances.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

没有返回值。

参见

  • MultipleIterator::next

MultipleIterator::setFlags

Sets flags

说明

public void MultipleIterator::setFlags ( int $flags )

Sets flags.

Warning

本函数还未编写文档,仅有参数列表。

参数

flags
The flags to set, according to the Flag Constants

返回值

没有返回值。

参见

  • Flag Constants
  • MultipleIterator::__construct
  • MultipleIterator::getFlags

MultipleIterator::valid

Checks the validity of sub iterators

说明

public bool MultipleIterator::valid ( void )

Checks the validity of sub iterators.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

Returns TRUE if one or all sub iterators are valid depending on flags, otherwise FALSE

参见

  • MultipleIterator::__construct

简介

This iterator ignores rewind operations. This allows processing an iterator in multiple partial foreach loops.

类摘要

NoRewindIterator

class NoRewindIterator extends IteratorIterator {

/* 方法 */

public __construct ( Iterator $iterator )

public mixed current ( void )

public iterator getInnerIterator ( void )

public mixed key ( void )

public void next ( void )

public void rewind ( void )

public bool valid ( void )

/* 继承的方法 */

public IteratorIterator::__construct ( Traversable $iterator )

public mixed IteratorIterator::current ( void )

public Traversable IteratorIterator::getInnerIterator ( void )

public mixed IteratorIterator::key ( void )

public void IteratorIterator::next ( void )

public void IteratorIterator::rewind ( void )

public bool IteratorIterator::valid ( void )

}

NoRewindIterator::__construct

Construct a NoRewindIterator

说明

public NoRewindIterator::__construct ( Iterator $iterator )

Constructs a NoRewindIterator.

参数

iterator
The iterator being used.

返回值

A NoRewindIterator based on the passed in iterator.

范例

示例 #1 NoRewindIterator::__construct example

The second loop does not output because the iterator is only used once, as it does not rewind.

<?php
$fruit = array('apple', 'banana', 'cranberry');

$arr = new ArrayObject($fruit);
$it  = new NoRewindIterator($arr->getIterator());

echo "Fruit A:\n";
foreach( $it as $item ) {
    echo $item . "\n";
}

echo "Fruit B:\n";
foreach( $it as $item ) {
    echo $item . "\n";
}
?>

以上例程的输出类似于:

Fruit A:
apple
banana
cranberry
Fruit B:

参见

  • NoRewindIterator::valid

NoRewindIterator::current

Get the current value

说明

public mixed NoRewindIterator::current ( void )

Gets the current value.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

The current value.

参见

  • NoRewindIterator::key

NoRewindIterator::getInnerIterator

Get the inner iterator

说明

public iterator NoRewindIterator::getInnerIterator ( void )

Gets the inner iterator, that was passed in to NoRewindIterator.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

The inner iterator, as passed to NoRewindIterator::__construct.

参见

  • NoRewindIterator::valid

NoRewindIterator::key

Get the current key

说明

public mixed NoRewindIterator::key ( void )

Gets the current key.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

The current key.

参见

  • NoRewindIterator::next

NoRewindIterator::next

Forward to the next element

说明

public void NoRewindIterator::next ( void )

Forwards to the next element.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

没有返回值。

参见

  • NoRewindIterator::rewind

NoRewindIterator::rewind

Prevents the rewind operation on the inner iterator

说明

public void NoRewindIterator::rewind ( void )

Prevents the rewind operation on the inner iterator.

参数

此函数没有参数。

返回值

没有返回值。

范例

示例 #1 NoRewindIterator::rewind example

This example demonstrates that calling rewind on a NoRewindIterator object has no effect.

<?php
$fruits = array("lemon", "orange", "apple", "pear");

$noRewindIterator = new NoRewindIterator(new ArrayIterator($fruits));

echo $noRewindIterator->current() . "\n";
$noRewindIterator->next();
// now rewind the iterator (nothing should happen)
$noRewindIterator->rewind();
echo $noRewindIterator->current() . "\n";
?>

以上例程会输出:

lemon
orange

NoRewindIterator::valid

Validates the iterator

说明

public bool NoRewindIterator::valid ( void )

Checks whether the iterator is valid.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

参见

  • NoRewindIterator::getInnerIterator

简介

This extended FilterIterator allows a recursive iteration using RecursiveIteratorIterator that only shows those elements which have children.

类摘要

ParentIterator

class ParentIterator extends RecursiveFilterIterator implements RecursiveIterator , OuterIterator {

/* 方法 */

public bool accept ( void )

public __construct ( RecursiveIterator $iterator )

public ParentIterator getChildren ( void )

public bool hasChildren ( void )

public void next ( void )

public void rewind ( void )

/* 继承的方法 */

public Iterator OuterIterator::getInnerIterator ( void )

}

ParentIterator::accept

Determines acceptability

说明

public bool ParentIterator::accept ( void )

Determines if the current element has children.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

TRUE if the current element is acceptable, otherwise FALSE.

参见

  • ParentIterator::hasChildren
  • FilterIterator::accept

ParentIterator::__construct

Constructs a ParentIterator

说明

public ParentIterator::__construct ( RecursiveIterator $iterator )

Constructs a ParentIterator on an iterator.

Warning

本函数还未编写文档,仅有参数列表。

参数

iterator
The iterator being constructed upon.

返回值

The ParentIterator.

ParentIterator::getChildren

Return the inner iterator's children contained in a ParentIterator

说明

public ParentIterator ParentIterator::getChildren ( void )

Get the inner iterator's children contained in a ParentIterator.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

An object.

ParentIterator::hasChildren

Check whether the inner iterator's current element has children

说明

public bool ParentIterator::hasChildren ( void )

Check whether the inner iterator's current element has children.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

ParentIterator::next

Move the iterator forward

说明

public void ParentIterator::next ( void )

Moves the iterator forward.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

没有返回值。

ParentIterator::rewind

Rewind the iterator

说明

public void ParentIterator::rewind ( void )

Rewinds the iterator.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

没有返回值。

简介

This iterator allows to unset and modify values and keys while iterating over Arrays and Objects in the same way as the ArrayIterator. Additionally it is possible to iterate over the current iterator entry.

类摘要

RecursiveArrayIterator

class RecursiveArrayIterator extends ArrayIterator implements RecursiveIterator {

/* 继承的常量 */

const integer STD_PROP_LIST = 1 ;

const integer ARRAY_AS_PROPS = 2 ;

/* 常量 */

const integer CHILD_ARRAYS_ONLY = 4 ;

/* 方法 */

public RecursiveArrayIterator getChildren ( void )

public bool hasChildren ( void )

/* Inherits */

public void ArrayIterator::append ( mixed $value )

public void ArrayIterator::asort ( void )

public ArrayIterator::__construct ([ mixed $array = array() [, int $flags = 0 ]] )

public int ArrayIterator::count ( void )

public mixed ArrayIterator::current ( void )

public array ArrayIterator::getArrayCopy ( void )

public int ArrayIterator::getFlags ( void )

public mixed ArrayIterator::key ( void )

public void ArrayIterator::ksort ( void )

public void ArrayIterator::natcasesort ( void )

public void ArrayIterator::natsort ( void )

public void ArrayIterator::next ( void )

public bool ArrayIterator::offsetExists ( mixed $index )

public mixed ArrayIterator::offsetGet ( mixed $index )

public void ArrayIterator::offsetSet ( mixed $index , mixed $newval )

public void ArrayIterator::offsetUnset ( mixed $index )

public void ArrayIterator::rewind ( void )

public void ArrayIterator::seek ( int $position )

public string ArrayIterator::serialize ( void )

public void ArrayIterator::setFlags ( string $flags )

public void ArrayIterator::uasort ( callable $cmp_function )

public void ArrayIterator::uksort ( callable $cmp_function )

public void ArrayIterator::unserialize ( string $serialized )

public bool ArrayIterator::valid ( void )

}

预定义常量

RecursiveArrayIterator Flags

RecursiveArrayIterator::CHILD_ARRAYS_ONLY
Treat only arrays (not objects) as having children for recursive iteration.

更新日志

版本说明
5.3.0CHILD_ARRAYS_ONLY flag was added.

RecursiveArrayIterator::getChildren

Returns an iterator for the current entry if it is an array or an object

说明

public RecursiveArrayIterator RecursiveArrayIterator::getChildren ( void )

Returns an iterator for the current iterator entry.

参数

此函数没有参数。

返回值

An iterator for the current entry, if it is an array or object.

错误/异常

An InvalidArgumentException will be thrown if the current entry does not contain an array or an object.

范例

示例 #1 RecursiveArrayIterator::getChildren example

<?php
$fruits = array("a" => "lemon", "b" => "orange", array("a" => "apple", "p" => "pear"));

$iterator = new RecursiveArrayIterator($fruits);

while ($iterator->valid()) {

    if ($iterator->hasChildren()) {
        // print all children
        foreach ($iterator->getChildren() as $key => $value) {
            echo $key . ' : ' . $value . "\n";
        }
    } else {
        echo "No children.\n";
    }

    $iterator->next();
}
?>

以上例程会输出:

No children.
No children.
a : apple
p : pear

参见

  • RecursiveArrayIterator::hasChildren

RecursiveArrayIterator::hasChildren

Returns whether current entry is an array or an object

说明

public bool RecursiveArrayIterator::hasChildren ( void )

Returns whether current entry is an array or an object for which an iterator can be obtained via RecursiveArrayIterator::getChildren.

参数

此函数没有参数。

返回值

Returns TRUE if the current entry is an array or an object, otherwise FALSE is returned.

范例

示例 #1 RecursiveArrayIterator::hasChildren example

<?php
$fruits = array("a" => "lemon", "b" => "orange", array("a" => "apple", "p" => "pear"));

$iterator = new RecursiveArrayIterator($fruits);

while ($iterator->valid()) {

    // Check if there are children
    if ($iterator->hasChildren()) {
        // print all children
        foreach ($iterator->getChildren() as $key => $value) {
            echo $key . ' : ' . $value . "\n";
        }
    } else {
        echo "No children.\n";
    }

    $iterator->next();
}
?>

以上例程会输出:

No children.
No children.
a : apple
p : pear

参见

  • RecursiveArrayIterator::getChildren

简介

...

类摘要

RecursiveCachingIterator

class RecursiveCachingIterator extends CachingIterator implements Countable , ArrayAccess , OuterIterator , RecursiveIterator {

/* 继承的常量 */

const integer CachingIterator::CALL_TOSTRING = 1 ;

const integer CachingIterator::CATCH_GET_CHILD = 16 ;

const integer CachingIterator::TOSTRING_USE_KEY = 2 ;

const integer CachingIterator::TOSTRING_USE_CURRENT = 4 ;

const integer CachingIterator::TOSTRING_USE_INNER = 8 ;

const integer CachingIterator::FULL_CACHE = 256 ;

/* 方法 */

public __construct ( Iterator $iterator [, int $flags = self::CALL_TOSTRING ] )

public RecursiveCachingIterator getChildren ( void )

public bool hasChildren ( void )

/* Inherits */

public CachingIterator::__construct ( Iterator $iterator [, int $flags = self::CALL_TOSTRING ] )

public int CachingIterator::count ( void )

public mixed CachingIterator::current ( void )

public array CachingIterator::getCache ( void )

public int CachingIterator::getFlags ( void )

public Iterator CachingIterator::getInnerIterator ( void )

public void CachingIterator::hasNext ( void )

public scalar CachingIterator::key ( void )

public void CachingIterator::next ( void )

public void CachingIterator::offsetExists ( mixed $index )

public void CachingIterator::offsetGet ( string $index )

public void CachingIterator::offsetSet ( mixed $index , mixed $newval )

public void CachingIterator::offsetUnset ( string $index )

public void CachingIterator::rewind ( void )

public void CachingIterator::setFlags ( int $flags )

public void CachingIterator::__toString ( void )

public void CachingIterator::valid ( void )

}

RecursiveCachingIterator::__construct

Construct

说明

public RecursiveCachingIterator::__construct ( Iterator $iterator [, int $flags = self::CALL_TOSTRING ] )

Constructs a new RecursiveCachingIterator, which consists of a passed in iterator.

Warning

本函数还未编写文档,仅有参数列表。

参数

iterator
The iterator being used.

flags
The flags. Use CALL_TOSTRING to call RecursiveCachingIterator::__toString for every element (the default), and/or CATCH_GET_CHILD to catch exceptions when trying to get children.

返回值

The RecursiveCachingIterator.

参见

  • CachingIterator::__construct

RecursiveCachingIterator::getChildren

Return the inner iterator's children as a RecursiveCachingIterator

说明

public RecursiveCachingIterator RecursiveCachingIterator::getChildren ( void )

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

The inner iterator's children, as a RecursiveCachingIterator.

RecursiveCachingIterator::hasChildren

Check whether the current element of the inner iterator has children

说明

public bool RecursiveCachingIterator::hasChildren ( void )

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

TRUE if the inner iterator has children, otherwise FALSE

简介

类摘要

RecursiveCallbackFilterIterator

class RecursiveCallbackFilterIterator extends CallbackFilterIterator implements OuterIterator , RecursiveIterator {

/* 方法 */

public __construct ( RecursiveIterator $iterator , string $callback )

public RecursiveCallbackFilterIterator getChildren ( void )

public bool hasChildren ( void )

/* 继承的方法 */

public bool CallbackFilterIterator::accept ( void )

}

范例

The callback should accept up to three arguments: the current item, the current key and the iterator, respectively.

示例 #1 Available callback arguments

<?php

/**
 * Callback for RecursiveCallbackFilterIterator
 *
 * @param $current   Current item's value
 * @param $key       Current item's key
 * @param $iterator  Iterator being filtered
 * @return boolean   TRUE to accept the current item, FALSE otherwise
 */
function my_callback($current, $key, $iterator) {
    // Your filtering code here
}

?>

Filtering a recursive iterator generally involves two conditions. The first is that, to allow recursion, the callback function should return TRUE if the current iterator item has children. The second is the normal filter condition, such as a file size or extension check as in the example below.

示例 #2 Recursive callback basic example

<?php

$dir = new RecursiveDirectoryIterator(__DIR__);

// Filter large files ( > 100MB)
$files = new RecursiveCallbackFilterIterator($dir, function ($current, $key, $iterator) {
    // Allow recursion
    if ($iterator->hasChildren()) {
        return TRUE;
    }
    // Check for large file
    if ($current->isFile() && $current->getSize() > 104857600) {
        return TRUE;
    }
    return FALSE;
});
 
foreach (new RecursiveIteratorIterator($files) as $file) {
    echo $file->getPathname() . PHP_EOL;
}

?>

RecursiveCallbackFilterIterator::__construct

Create a RecursiveCallbackFilterIterator from a RecursiveIterator

说明

public RecursiveCallbackFilterIterator::__construct ( RecursiveIterator $iterator , string $callback )

Creates a filtered iterator from a RecursiveIterator using the callback to determine which items are accepted or rejected.

参数

iterator
The recursive iterator to be filtered.

callback
The callback, which should return TRUE to accept the current item or FALSE otherwise. See Examples.

May be any valid callable value.

返回值

没有返回值。

参见

RecursiveCallbackFilterIterator::getChildren

Return the inner iterator's children contained in a RecursiveCallbackFilterIterator

说明

public RecursiveCallbackFilterIterator RecursiveCallbackFilterIterator::getChildren ( void )

Fetches the filtered children of the inner iterator.

RecursiveCallbackFilterIterator::hasChildren should be used to determine if there are children to be fetched.

参数

此函数没有参数。

返回值

Returns a RecursiveCallbackFilterIterator containing the children.

参见

RecursiveCallbackFilterIterator::hasChildren

Check whether the inner iterator's current element has children

说明

public bool RecursiveCallbackFilterIterator::hasChildren ( void )

Returns TRUE if the current element has children, FALSE otherwise.

参数

此函数没有参数。

返回值

Returns TRUE if the current element has children, FALSE otherwise.

范例

示例 #1 RecursiveCallbackFilterIterator::hasChildren basic usage

<?php

$dir = new RecursiveDirectoryIterator(__DIR__);

// Recursively iterate over XML files
$files = new RecursiveCallbackFilterIterator($dir, function ($current, $key, $iterator) {
    // Allow recursion into directories
    if ($iterator->hasChildren()) {
        return TRUE;
    }
    // Check for XML file
    if (!strcasecmp($current->getExtension(), 'xml')) {
        return TRUE;
    }
    return FALSE;
});

?>

参见

简介

The RecursiveDirectoryIterator provides an interface for iterating recursively over filesystem directories.

类摘要

RecursiveDirectoryIterator

class RecursiveDirectoryIterator extends FilesystemIterator implements SeekableIterator , RecursiveIterator {

/* 继承的常量 */

const integer FilesystemIterator::CURRENT_AS_PATHNAME = 32 ;

const integer FilesystemIterator::CURRENT_AS_FILEINFO = 0 ;

const integer FilesystemIterator::CURRENT_AS_SELF = 16 ;

const integer FilesystemIterator::CURRENT_MODE_MASK = 240 ;

const integer FilesystemIterator::KEY_AS_PATHNAME = 0 ;

const integer FilesystemIterator::KEY_AS_FILENAME = 256 ;

const integer FilesystemIterator::FOLLOW_SYMLINKS = 512 ;

const integer FilesystemIterator::KEY_MODE_MASK = 3840 ;

const integer FilesystemIterator::NEW_CURRENT_AND_KEY = 256 ;

const integer FilesystemIterator::SKIP_DOTS = 4096 ;

const integer FilesystemIterator::UNIX_PATHS = 8192 ;

/* 方法 */

public __construct ( string $path [, int $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO ] )

public mixed getChildren ( void )

public string getSubPath ( void )

public string getSubPathname ( void )

public bool hasChildren ([ bool $allow_links = FALSE ] )

public string key ( void )

public void next ( void )

public void rewind ( void )

/* Inherits */

public FilesystemIterator::__construct ( string $path [, int $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS ] )

public mixed FilesystemIterator::current ( void )

public int FilesystemIterator::getFlags ( void )

public string FilesystemIterator::key ( void )

public void FilesystemIterator::next ( void )

public void FilesystemIterator::rewind ( void )

public void FilesystemIterator::setFlags ([ int $flags ] )

}

更新日志

版本说明
5.3.0The FilesystemIterator was introduced as the parent class. Previously, the parent was the DirectoryIterator.
5.3.0Implements SeekableIterator.
5.2.11, 5.3.1Added RecursiveDirectoryIterator::FOLLOW_SYMLINKS

RecursiveDirectoryIterator::__construct

Constructs a RecursiveDirectoryIterator

说明

public RecursiveDirectoryIterator::__construct ( string $path [, int $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO ] )

Constructs a RecursiveDirectoryIterator for the provided path.

参数

path
The path of the directory to be iterated over.

flags
Flags may be provided which will affect the behavior of some methods. A list of the flags can found under FilesystemIterator predefined constants. They can also be set later with FilesystemIterator::setFlags.

返回值

Returns the newly created RecursiveDirectoryIterator.

错误/异常

Throws an UnexpectedValueException if the path cannot be found or is not a directory.

范例

示例 #1 RecursiveDirectoryIterator example

<?php

$directory = '/tmp';

$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));

$it->rewind();
while($it->valid()) {

    if (!$it->isDot()) {
        echo 'SubPathName: ' . $it->getSubPathName() . "\n";
        echo 'SubPath:     ' . $it->getSubPath() . "\n";
        echo 'Key:         ' . $it->key() . "\n\n";
    }

    $it->next();
}

?>

以上例程的输出类似于:

SubPathName: fruit/apple.xml
SubPath:     fruit
Key:         /tmp/fruit/apple.xml

SubPathName: stuff.xml
SubPath:     
Key:         /tmp/stuff.xml

SubPathName: veggies/carrot.xml
SubPath:     veggies
Key:         /tmp/veggies/carrot.xml

参见

RecursiveDirectoryIterator::getChildren

Returns an iterator for the current entry if it is a directory

说明

public mixed RecursiveDirectoryIterator::getChildren ( void )

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

The filename, file information, or $this depending on the set flags. See the FilesystemIterator constants.

RecursiveDirectoryIterator::getSubPath

Get sub path

说明

public string RecursiveDirectoryIterator::getSubPath ( void )

Returns the sub path relative to the directory given in the constructor.

参数

此函数没有参数。

返回值

The sub path.

范例

示例 #1 getSubPath example

$directory = '/tmp';
      
      $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
      
      foreach ($it as $file) {
          echo 'SubPathName: ' . $it->getSubPathName() . "\n";
          echo 'SubPath:     ' . $it->getSubPath() . "\n\n";
      }

以上例程的输出类似于:

     SubPathName: fruit/apple.xml
     SubPath:     fruit
     
     SubPathName: stuff.xml
     SubPath:     
     
     SubPathName: veggies/carrot.xml
     SubPath:     veggies

参见

  • RecursiveDirectoryIterator::getSubPathName
  • RecursiveDirectoryIterator::key

RecursiveDirectoryIterator::getSubPathname

Get sub path and name

说明

public string RecursiveDirectoryIterator::getSubPathname ( void )

Gets the sub path and filename.

参数

此函数没有参数。

返回值

The sub path (sub directory) and filename.

范例

示例 #1 getSubPathname example

$directory = '/tmp';
      
      $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory));
      
      foreach ($it as $file) {
          echo 'SubPathName: ' . $it->getSubPathName() . "\n";
          echo 'SubPath:     ' . $it->getSubPath() . "\n\n";
      }

以上例程的输出类似于:

     SubPathName: fruit/apple.xml
     SubPath:     fruit
     
     SubPathName: stuff.xml
     SubPath:     
     
     SubPathName: veggies/carrot.xml
     SubPath:     veggies

参见

  • RecursiveDirectoryIterator::getSubPath
  • RecursiveDirectoryIterator::key

RecursiveDirectoryIterator::hasChildren

Returns whether current entry is a directory and not '.' or '..'

说明

public bool RecursiveDirectoryIterator::hasChildren ([ bool $allow_links = FALSE ] )

Warning

本函数还未编写文档,仅有参数列表。

参数

allow_links

返回值

Returns whether the current entry is a directory, but not '.' or '..'

RecursiveDirectoryIterator::key

Return path and filename of current dir entry

说明

public string RecursiveDirectoryIterator::key ( void )

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

The path and filename of the current dir entry.

RecursiveDirectoryIterator::next

Move to next entry

说明

public void RecursiveDirectoryIterator::next ( void )

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

没有返回值。

RecursiveDirectoryIterator::rewind

Rewind dir back to the start

说明

public void RecursiveDirectoryIterator::rewind ( void )

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

没有返回值。

简介

This abstract iterator filters out unwanted values for a RecursiveIterator. This class should be extended to implement custom filters. The RecursiveFilterIterator::accept must be implemented in the subclass.

类摘要

RecursiveFilterIterator

abstract class RecursiveFilterIterator extends FilterIterator implements OuterIterator , RecursiveIterator {

/* 方法 */

public __construct ( RecursiveIterator $iterator )

public RecursiveFilterIterator getChildren ( void )

public bool hasChildren ( void )

/* 继承的方法 */

public abstract bool FilterIterator::accept ( void )

public FilterIterator::__construct ( Iterator $iterator )

public mixed FilterIterator::current ( void )

public Iterator FilterIterator::getInnerIterator ( void )

public mixed FilterIterator::key ( void )

public void FilterIterator::next ( void )

public void FilterIterator::rewind ( void )

public bool FilterIterator::valid ( void )

}

RecursiveFilterIterator::__construct

Create a RecursiveFilterIterator from a RecursiveIterator

说明

public RecursiveFilterIterator::__construct ( RecursiveIterator $iterator )

Create a RecursiveFilterIterator from a RecursiveIterator.

参数

iterator
The RecursiveIterator to be filtered.

返回值

没有返回值。

范例

示例 #1 Basic RecursiveFilterIterator example

<?php
class TestsOnlyFilter extends RecursiveFilterIterator {
    public function accept() {
        // Accept the current item if we can recurse into it
        // or it is a value starting with "test"
        return $this->hasChildren() || (strpos($this->current(), "test") !== FALSE);
    }
}

$array    = array("test1", array("taste2", "test3", "test4"), "test5");
$iterator = new RecursiveArrayIterator($array);
$filter   = new TestsOnlyFilter($iterator);

foreach(new RecursiveIteratorIterator($filter) as $key => $value)
{
    echo $value . "\n";
}
?>

以上例程的输出类似于:

test1
test3
test4
test5

示例 #2 RecursiveFilterIterator example

<?php
class StartsWithFilter extends RecursiveFilterIterator {

    protected $word;

    public function __construct(RecursiveIterator $rit, $word) {
        $this->word = $word;
        parent::__construct($rit);
    }

    public function accept() {
        return $this->hasChildren() OR strpos($this->current(), $this->word) === 0;
    }
    
    public function getChildren() {
        return new self($this->getInnerIterator()->getChildren(), $this->word);
    }
}

$array    = array("test1", array("taste2", "test3", "test4"), "test5");
$iterator = new RecursiveArrayIterator($array);
$filter   = new StartsWithFilter($iterator, "test");

foreach(new RecursiveIteratorIterator($filter) as $key => $value)
{
    echo $value . "\n";
}
?>

以上例程的输出类似于:

test1
test3
test4
test5

参见

  • RecursiveFilterIterator::getChildren
  • RecursiveFilterIterator::hasChildren
  • FilterIterator::accept

RecursiveFilterIterator::getChildren

Return the inner iterator's children contained in a RecursiveFilterIterator

说明

public RecursiveFilterIterator RecursiveFilterIterator::getChildren ( void )

Return the inner iterator's children contained in a RecursiveFilterIterator.

参数

此函数没有参数。

返回值

Returns a RecursiveFilterIterator containing the inner iterator's children.

参见

  • RecursiveFilterIterator::hasChildren
  • RecursiveIterator::getChildren

RecursiveFilterIterator::hasChildren

Check whether the inner iterator's current element has children

说明

public bool RecursiveFilterIterator::hasChildren ( void )

Check whether the inner iterator's current element has children.

参数

此函数没有参数。

返回值

TRUE if the inner iterator has children, otherwise FALSE

参见

  • RecursiveFilterIterator::getChildren
  • RecursiveIterator::hasChildren

简介

Can be used to iterate through recursive iterators.

类摘要

RecursiveIteratorIterator

class RecursiveIteratorIterator implements OuterIterator {

/* 常量 */

const integer RecursiveIteratorIterator::LEAVES_ONLY = 0 ;

const integer RecursiveIteratorIterator::SELF_FIRST = 1 ;

const integer RecursiveIteratorIterator::CHILD_FIRST = 2 ;

const integer RecursiveIteratorIterator::CATCH_GET_CHILD = 16 ;

/* 方法 */

public void beginChildren ( void )

public void beginIteration ( void )

public RecursiveIterator callGetChildren ( void )

public bool callHasChildren ( void )

public __construct ( Traversable $iterator [, int $mode = RecursiveIteratorIterator::LEAVES_ONLY [, int $flags = 0 ]] )

public mixed current ( void )

public void endChildren ( void )

public void endIteration ( void )

public int getDepth ( void )

public iterator getInnerIterator ( void )

public mixed getMaxDepth ( void )

public RecursiveIterator getSubIterator ([ int $level ] )

public mixed key ( void )

public void next ( void )

public void nextElement ( void )

public void rewind ( void )

public void setMaxDepth ([ int $max_depth = -1 ] )

public bool valid ( void )

/* 继承的方法 */

public Iterator OuterIterator::getInnerIterator ( void )

}

预定义常量

RecursiveIteratorIterator::LEAVES_ONLY

RecursiveIteratorIterator::SELF_FIRST

RecursiveIteratorIterator::CHILD_FIRST

RecursiveIteratorIterator::CATCH_GET_CHILD

RecursiveIteratorIterator::beginChildren

Begin children

说明

public void RecursiveIteratorIterator::beginChildren ( void )

Is called after calling RecursiveIteratorIterator::getChildren, and its associated RecursiveIteratorIterator::rewind.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

没有返回值。

RecursiveIteratorIterator::beginIteration

Begin Iteration

说明

public void RecursiveIteratorIterator::beginIteration ( void )

Called when iteration begins (after the first RecursiveIteratorIterator::rewind call.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

没有返回值。

RecursiveIteratorIterator::callGetChildren

Get children

说明

public RecursiveIterator RecursiveIteratorIterator::callGetChildren ( void )

Get children of the current element.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

A RecursiveIterator.

RecursiveIteratorIterator::callHasChildren

Has children

说明

public bool RecursiveIteratorIterator::callHasChildren ( void )

Called for each element to test whether it has children.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

TRUE if the element has children, otherwise FALSE

RecursiveIteratorIterator::__construct

Construct a RecursiveIteratorIterator

说明

public RecursiveIteratorIterator::__construct ( Traversable $iterator [, int $mode = RecursiveIteratorIterator::LEAVES_ONLY [, int $flags = 0 ]] )

Creates a RecursiveIteratorIterator from a RecursiveIterator.

参数

iterator
The iterator being constructed from. Either a RecursiveIterator or IteratorAggregate.

mode
Optional mode. Possible values are

  • RecursiveIteratorIterator::LEAVES_ONLY - The default. Lists only leaves in iteration.
  • RecursiveIteratorIterator::SELF_FIRST - Lists leaves and parents in iteration with parents coming first.
  • RecursiveIteratorIterator::CHILD_FIRST - Lists leaves and parents in iteration with leaves coming first.

flags
Optional flag. Possible values are RecursiveIteratorIterator::CATCH_GET_CHILD which will then ignore exceptions thrown in calls to RecursiveIteratorIterator::getChildren.

返回值

没有返回值。

范例

示例 #1 Iterating a RecursiveIteratorIterator

<?php
$array = array(
    array(
        array(
            array(
                'leaf-0-0-0-0',
                'leaf-0-0-0-1'
            ),
            'leaf-0-0-0'
        ),
        array(
            array(
                'leaf-0-1-0-0',
                'leaf-0-1-0-1'
            ),
            'leaf-0-1-0'
        ),
        'leaf-0-0'
    )
);

$iterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator($array),
    $mode
);
foreach ($iterator as $key => $leaf) {
    echo "$key => $leaf", PHP_EOL;
}
?>

Output with $mode = RecursiveIteratorIterator::LEAVES_ONLY

0 => leaf-0-0-0-0
1 => leaf-0-0-0-1
0 => leaf-0-0-0
0 => leaf-0-1-0-0
1 => leaf-0-1-0-1
0 => leaf-0-1-0
0 => leaf-0-0

Output with $mode = RecursiveIteratorIterator::SELF_FIRST

0 => Array
0 => Array
0 => Array
0 => leaf-0-0-0-0
1 => leaf-0-0-0-1
1 => leaf-0-0-0
1 => Array
0 => Array
0 => leaf-0-1-0-0
1 => leaf-0-1-0-1
1 => leaf-0-1-0
2 => leaf-0-0

Output with $mode = RecursiveIteratorIterator::CHILD_FIRST

0 => leaf-0-0-0-0
1 => leaf-0-0-0-1
0 => Array
1 => leaf-0-0-0
0 => Array
0 => leaf-0-1-0-0
1 => leaf-0-1-0-1
0 => Array
1 => leaf-0-1-0
1 => Array
2 => leaf-0-0
0 => Array

RecursiveIteratorIterator::current

Access the current element value

说明

public mixed RecursiveIteratorIterator::current ( void )

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

The current elements value.

RecursiveIteratorIterator::endChildren

End children

说明

public void RecursiveIteratorIterator::endChildren ( void )

Called when end recursing one level.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

没有返回值。

RecursiveIteratorIterator::endIteration

End Iteration

说明

public void RecursiveIteratorIterator::endIteration ( void )

Called when the iteration ends (when RecursiveIteratorIterator::valid first returns FALSE.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

没有返回值。

RecursiveIteratorIterator::getDepth

Get the current depth of the recursive iteration

说明

public int RecursiveIteratorIterator::getDepth ( void )

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

The current depth of the recursive iteration.

RecursiveIteratorIterator::getInnerIterator

Get inner iterator

说明

public iterator RecursiveIteratorIterator::getInnerIterator ( void )

Gets the current active sub iterator.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

The current active sub iterator.

RecursiveIteratorIterator::getMaxDepth

Get max depth

说明

public mixed RecursiveIteratorIterator::getMaxDepth ( void )

Gets the maximum allowable depth.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

The maximum accepted depth, or FALSE if any depth is allowed.

参见

  • RecursiveIteratorIterator::setMaxDepth

RecursiveIteratorIterator::getSubIterator

The current active sub iterator

说明

public RecursiveIterator RecursiveIteratorIterator::getSubIterator ([ int $level ] )

Warning

本函数还未编写文档,仅有参数列表。

参数

level

返回值

The current active sub iterator.

RecursiveIteratorIterator::key

Access the current key

说明

public mixed RecursiveIteratorIterator::key ( void )

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

The current key.

RecursiveIteratorIterator::next

Move forward to the next element

说明

public void RecursiveIteratorIterator::next ( void )

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

没有返回值。

RecursiveIteratorIterator::nextElement

Next element

说明

public void RecursiveIteratorIterator::nextElement ( void )

Called when the next element is available.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

没有返回值。

RecursiveIteratorIterator::rewind

Rewind the iterator to the first element of the top level inner iterator

说明

public void RecursiveIteratorIterator::rewind ( void )

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

没有返回值。

RecursiveIteratorIterator::setMaxDepth

Set max depth

说明

public void RecursiveIteratorIterator::setMaxDepth ([ int $max_depth = -1 ] )

Set the maximum allowed depth.

Warning

本函数还未编写文档,仅有参数列表。

参数

max_depth
The maximum allowed depth. -1 is used for any depth.

返回值

没有返回值。

错误/异常

Emits an Exception if max_depth is less than -1.

RecursiveIteratorIterator::valid

Check whether the current position is valid

说明

public bool RecursiveIteratorIterator::valid ( void )

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

TRUE if the current position is valid, otherwise FALSE

简介

This recursive iterator can filter another recursive iterator via a regular expression.

类摘要

RecursiveRegexIterator

class RecursiveRegexIterator extends RegexIterator implements RecursiveIterator {

/* 继承的常量 */

const integer MATCH = 0 ;

const integer GET_MATCH = 1 ;

const integer ALL_MATCHES = 2 ;

const integer SPLIT = 3 ;

const integer REPLACE = 4 ;

const integer USE_KEY = 1 ;

/* 方法 */

public __construct ( RecursiveIterator $iterator , string $regex [, int $mode = self::MATCH [, int $flags = 0 [, int $preg_flags = 0 ]]] )

public RecursiveRegexIterator getChildren ( void )

public bool hasChildren ( void )

/* 继承的方法 */

public RecursiveIterator RecursiveIterator::getChildren ( void )

public bool RecursiveIterator::hasChildren ( void )

public bool RegexIterator::accept ( void )

public int RegexIterator::getFlags ( void )

public int RegexIterator::getMode ( void )

public int RegexIterator::getPregFlags ( void )

public string RegexIterator::getRegex ( void )

public void RegexIterator::setFlags ( int $flags )

public void RegexIterator::setMode ( int $mode )

public void RegexIterator::setPregFlags ( int $preg_flags )

}

RecursiveRegexIterator::__construct

Creates a new RecursiveRegexIterator

说明

public RecursiveRegexIterator::__construct ( RecursiveIterator $iterator , string $regex [, int $mode = self::MATCH [, int $flags = 0 [, int $preg_flags = 0 ]]] )

Creates a new regular expression iterator.

参数

iterator
The recursive iterator to apply this regex filter to.

regex
The regular expression to match.

mode
Operation mode, see RegexIterator::setMode for a list of modes.

flags
Special flags, see RegexIterator::setFlags for a list of available flags.

preg_flags
The regular expression flags. These flags depend on the operation mode parameter:

operation modeavailable flags
RecursiveRegexIterator::ALL_MATCHESSee preg_match_all.
RecursiveRegexIterator::GET_MATCHSee preg_match.
RecursiveRegexIterator::MATCHSee preg_match.
RecursiveRegexIterator::REPLACEnone.
RecursiveRegexIterator::SPLITSee preg_split.

范例

示例 #1 RecursiveRegexIterator::__construct example

Creates a new RegexIterator that filters all strings that start with 'test'.

<?php
$rArrayIterator = new RecursiveArrayIterator(array('test1', array('tet3', 'test4', 'test5')));
$rRegexIterator = new RecursiveRegexIterator($rArrayIterator, '/^test/',
    RecursiveRegexIterator::ALL_MATCHES);

foreach ($rRegexIterator as $key1 => $value1) {

    if ($rRegexIterator->hasChildren()) {

        // print all children
        echo "Children: ";
        foreach ($rRegexIterator->getChildren() as $key => $value) {
            echo $value . " ";
        }
        echo "\n";
    } else {
        echo "No children\n";
    }

}
?>

以上例程的输出类似于:

No children
Children: test4 test5

参见

  • preg_match
  • preg_match_all
  • preg_replace
  • preg_split

RecursiveRegexIterator::getChildren

Returns an iterator for the current entry

说明

public RecursiveRegexIterator RecursiveRegexIterator::getChildren ( void )

Returns an iterator for the current iterator entry.

参数

此函数没有参数。

返回值

An iterator for the current entry, if it can be iterated over by the inner iterator.

错误/异常

An InvalidArgumentException will be thrown if the current entry does not contain a value that can be iterated over by the inner iterator.

范例

示例 #1 RecursiveRegexIterator::getChildren example

<?php
$rArrayIterator = new RecursiveArrayIterator(array('test1', array('tet3', 'test4', 'test5')));
$rRegexIterator = new RecursiveRegexIterator($rArrayIterator, '/^test/',
    RecursiveRegexIterator::ALL_MATCHES);

foreach ($rRegexIterator as $key1 => $value1) {

    if ($rRegexIterator->hasChildren()) {

        // print all children
        echo "Children: ";
        foreach ($rRegexIterator->getChildren() as $key => $value) {
            echo $value . " ";
        }
        echo "\n";
    } else {
        echo "No children\n";
    }

}
?>

以上例程会输出:

No children
Children: test4 test5

参见

  • RecursiveRegexIterator::hasChildren

RecursiveRegexIterator::hasChildren

Returns whether an iterator can be obtained for the current entry

说明

public bool RecursiveRegexIterator::hasChildren ( void )

Returns whether an iterator can be obtained for the current entry. This iterator can be obtained via RecursiveRegexIterator::getChildren.

参数

此函数没有参数。

返回值

Returns TRUE if an iterator can be obtained for the current entry, otherwise returns FALSE.

范例

示例 #1 RecursiveRegexIterator::hasChildren example

<?php
$rArrayIterator = new RecursiveArrayIterator(array('test1', array('tet3', 'test4', 'test5')));
$rRegexIterator = new RecursiveRegexIterator($rArrayIterator, '/^test/',
    RecursiveRegexIterator::ALL_MATCHES);

foreach ($rRegexIterator as $value) {
    var_dump($rRegexIterator->hasChildren());
}
?>

以上例程会输出:

bool(false)
bool(true)

参见

  • RecursiveRegexIterator::getChildren

简介

Allows iterating over a RecursiveIterator to generate an ASCII graphic tree.

类摘要

RecursiveTreeIterator

class RecursiveTreeIterator extends RecursiveIteratorIterator implements OuterIterator {

/* 继承的常量 */

const integer RecursiveIteratorIterator::LEAVES_ONLY = 0 ;

const integer RecursiveIteratorIterator::SELF_FIRST = 1 ;

const integer RecursiveIteratorIterator::CHILD_FIRST = 2 ;

const integer RecursiveIteratorIterator::CATCH_GET_CHILD = 16 ;

/* 常量 */

const integer RecursiveTreeIterator::BYPASS_CURRENT = 4 ;

const integer RecursiveTreeIterator::BYPASS_KEY = 8 ;

const integer RecursiveTreeIterator::PREFIX_LEFT = 0 ;

const integer RecursiveTreeIterator::PREFIX_MID_HAS_NEXT = 1 ;

const integer RecursiveTreeIterator::PREFIX_MID_LAST = 2 ;

const integer RecursiveTreeIterator::PREFIX_END_HAS_NEXT = 3 ;

const integer RecursiveTreeIterator::PREFIX_END_LAST = 4 ;

const integer RecursiveTreeIterator::PREFIX_RIGHT = 5 ;

/* 方法 */

public void beginChildren ( void )

public RecursiveIterator beginIteration ( void )

public RecursiveIterator callGetChildren ( void )

public bool callHasChildren ( void )

public __construct ( RecursiveIterator|IteratorAggregate $it [, int $flags = RecursiveTreeIterator::BYPASS_KEY [, int $cit_flags = CachingIterator::CATCH_GET_CHILD [, int $mode = RecursiveIteratorIterator::SELF_FIRST ]]] )

public string current ( void )

public void endChildren ( void )

public void endIteration ( void )

public string getEntry ( void )

public string getPostfix ( void )

public string getPrefix ( void )

public string key ( void )

public void next ( void )

public void nextElement ( void )

public void rewind ( void )

public void setPostfix ( string $postfix )

public void setPrefixPart ( int $part , string $value )

public bool valid ( void )

/* 继承的方法 */

public void RecursiveIteratorIterator::beginChildren ( void )

public void RecursiveIteratorIterator::beginIteration ( void )

public RecursiveIterator RecursiveIteratorIterator::callGetChildren ( void )

public bool RecursiveIteratorIterator::callHasChildren ( void )

public RecursiveIteratorIterator::__construct ( Traversable $iterator [, int $mode = RecursiveIteratorIterator::LEAVES_ONLY [, int $flags = 0 ]] )

public mixed RecursiveIteratorIterator::current ( void )

public void RecursiveIteratorIterator::endChildren ( void )

public void RecursiveIteratorIterator::endIteration ( void )

public int RecursiveIteratorIterator::getDepth ( void )

public iterator RecursiveIteratorIterator::getInnerIterator ( void )

public mixed RecursiveIteratorIterator::getMaxDepth ( void )

public RecursiveIterator RecursiveIteratorIterator::getSubIterator ([ int $level ] )

public mixed RecursiveIteratorIterator::key ( void )

public void RecursiveIteratorIterator::next ( void )

public void RecursiveIteratorIterator::nextElement ( void )

public void RecursiveIteratorIterator::rewind ( void )

public void RecursiveIteratorIterator::setMaxDepth ([ int $max_depth = -1 ] )

public bool RecursiveIteratorIterator::valid ( void )

}

预定义常量

RecursiveTreeIterator::BYPASS_CURRENT

RecursiveTreeIterator::BYPASS_KEY

RecursiveTreeIterator::PREFIX_LEFT

RecursiveTreeIterator::PREFIX_MID_HAS_NEXT

RecursiveTreeIterator::PREFIX_MID_LAST

RecursiveTreeIterator::PREFIX_END_HAS_NEXT

RecursiveTreeIterator::PREFIX_END_LAST

RecursiveTreeIterator::PREFIX_RIGHT

RecursiveTreeIterator::beginChildren

Begin children

说明

public void RecursiveTreeIterator::beginChildren ( void )

Called when recursing one level down.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

没有返回值。

RecursiveTreeIterator::beginIteration

Begin iteration

说明

public RecursiveIterator RecursiveTreeIterator::beginIteration ( void )

Called when iteration begins (after the first RecursiveTreeIterator::rewind call).

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

A RecursiveIterator.

RecursiveTreeIterator::callGetChildren

Get children

说明

public RecursiveIterator RecursiveTreeIterator::callGetChildren ( void )

Gets children of the current element.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

A RecursiveIterator.

RecursiveTreeIterator::callHasChildren

Has children

说明

public bool RecursiveTreeIterator::callHasChildren ( void )

Called for each element to test whether it has children.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

TRUE if there are children, otherwise FALSE

RecursiveTreeIterator::__construct

Construct a RecursiveTreeIterator

说明

public RecursiveTreeIterator::__construct ( RecursiveIterator|IteratorAggregate $it [, int $flags = RecursiveTreeIterator::BYPASS_KEY [, int $cit_flags = CachingIterator::CATCH_GET_CHILD [, int $mode = RecursiveIteratorIterator::SELF_FIRST ]]] )

Constructs a new RecursiveTreeIterator from the supplied recursive iterator.

Warning

本函数还未编写文档,仅有参数列表。

参数

it
The RecursiveIterator or IteratorAggregate to iterate over.

flags
Flags may be provided which will affect the behavior of some methods. A list of the flags can found under RecursiveTreeIterator predefined constants.

caching_it_flags
Flags to affect the behavior of the RecursiveCachingIterator used internally.

mode
Flags to affect the behavior of the RecursiveIteratorIterator used internally.

返回值

没有返回值。

RecursiveTreeIterator::current

Get current element

说明

public string RecursiveTreeIterator::current ( void )

Gets the current element prefixed and postfixed.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

Returns the current element prefixed and postfixed.

RecursiveTreeIterator::endChildren

End children

说明

public void RecursiveTreeIterator::endChildren ( void )

Called when end recursing one level.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

没有返回值。

RecursiveTreeIterator::endIteration

End iteration

说明

public void RecursiveTreeIterator::endIteration ( void )

Called when the iteration ends (when RecursiveTreeIterator::valid first returns FALSE)

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

没有返回值。

RecursiveTreeIterator::getEntry

Get current entry

说明

public string RecursiveTreeIterator::getEntry ( void )

Gets the part of the tree built for the current element.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

Returns the part of the tree built for the current element.

RecursiveTreeIterator::getPostfix

Get the postfix

说明

public string RecursiveTreeIterator::getPostfix ( void )

Gets the string to place after the current element.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

Returns the string to place after the current element.

RecursiveTreeIterator::getPrefix

Get the prefix

说明

public string RecursiveTreeIterator::getPrefix ( void )

Gets the string to place in front of current element

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

Returns the string to place in front of current element

RecursiveTreeIterator::key

Get the key of the current element

说明

public string RecursiveTreeIterator::key ( void )

Gets the current key prefixed and postfixed.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

Returns the current key prefixed and postfixed.

RecursiveTreeIterator::next

Move to next element

说明

public void RecursiveTreeIterator::next ( void )

Moves forward to the next element.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

没有返回值。

RecursiveTreeIterator::nextElement

Next element

说明

public void RecursiveTreeIterator::nextElement ( void )

Called when the next element is available.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

没有返回值。

RecursiveTreeIterator::rewind

Rewind iterator

说明

public void RecursiveTreeIterator::rewind ( void )

Rewinds the iterator to the first element of the top level inner iterator.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

没有返回值。

RecursiveTreeIterator::setPostfix

Set postfix

说明

public void RecursiveTreeIterator::setPostfix ( string $postfix )

Sets postfix as used in RecursiveTreeIterator::getPostfix.

Warning

本函数还未编写文档,仅有参数列表。

参数

postfix

返回值

没有返回值。

RecursiveTreeIterator::setPrefixPart

Set a part of the prefix

说明

public void RecursiveTreeIterator::setPrefixPart ( int $part , string $value )

Sets a part of the prefix used in the graphic tree.

Warning

本函数还未编写文档,仅有参数列表。

参数

part
One of the RecursiveTreeIterator::PREFIX_* constants.

value
The value to assign to the part of the prefix specified in part.

返回值

没有返回值。

RecursiveTreeIterator::valid

Check validity

说明

public bool RecursiveTreeIterator::valid ( void )

Check whether the current position is valid.

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

TRUE if the current position is valid, otherwise FALSE

简介

This iterator can be used to filter another iterator based on a regular expression.

类摘要

RegexIterator

class RegexIterator extends FilterIterator {

/* 常量 */

const integer MATCH = 0 ;

const integer GET_MATCH = 1 ;

const integer ALL_MATCHES = 2 ;

const integer SPLIT = 3 ;

const integer REPLACE = 4 ;

const integer USE_KEY = 1 ;

/* 方法 */

public __construct ( Iterator $iterator , string $regex [, int $mode = self::MATCH [, int $flags = 0 [, int $preg_flags = 0 ]]] )

public bool accept ( void )

public int getFlags ( void )

public int getMode ( void )

public int getPregFlags ( void )

public string getRegex ( void )

public void setFlags ( int $flags )

public void setMode ( int $mode )

public void setPregFlags ( int $preg_flags )

/* 继承的方法 */

public abstract bool FilterIterator::accept ( void )

public FilterIterator::__construct ( Iterator $iterator )

public mixed FilterIterator::current ( void )

public Iterator FilterIterator::getInnerIterator ( void )

public mixed FilterIterator::key ( void )

public void FilterIterator::next ( void )

public void FilterIterator::rewind ( void )

public bool FilterIterator::valid ( void )

}

预定义常量

RegexIterator operation modes

RegexIterator::ALL_MATCHES
Return all matches for the current entry (see preg_match_all).

RegexIterator::GET_MATCH
Return the first match for the current entry (see preg_match).

RegexIterator::MATCH
Only execute match (filter) for the current entry (see preg_match).

RegexIterator::REPLACE
Replace the current entry (see preg_replace; Not fully implemented yet)

RegexIterator::SPLIT
Returns the split values for the current entry (see preg_split).

RegexIterator Flags

RegexIterator::USE_KEY
Special flag: Match the entry key instead of the entry value.

RegexIterator::accept

Get accept status

说明

public bool RegexIterator::accept ( void )

Matches (string) RegexIterator::current (or RegexIterator::key if the RegexIterator::USE_KEY flag is set) against the regular expression.

参数

此函数没有参数。

返回值

TRUE if a match, FALSE otherwise.

范例

示例 #1 RegexIterator::accept example

This example shows that only items matching the regular expression are accepted.

<?php
$names = new ArrayIterator(array('Ann', 'Bob', 'Charlie', 'David'));
$filter = new RegexIterator($names, '/^[B-D]/');
foreach ($filter as $name) {
    echo $name . PHP_EOL;
}
?>

以上例程会输出:

Bob
Charlie
David

参见

RegexIterator::__construct

Create a new RegexIterator

说明

public RegexIterator::__construct ( Iterator $iterator , string $regex [, int $mode = self::MATCH [, int $flags = 0 [, int $preg_flags = 0 ]]] )

Create a new RegexIterator which filters an Iterator using a regular expression.

参数

iterator
The iterator to apply this regex filter to.

regex
The regular expression to match.

mode
Operation mode, see RegexIterator::setMode for a list of modes.

flags
Special flags, see RegexIterator::setFlags for a list of available flags.

preg_flags
The regular expression flags. These flags depend on the operation mode parameter:

operation modeavailable flags
RegexIterator::ALL_MATCHESSee preg_match_all.
RegexIterator::GET_MATCHSee preg_match.
RegexIterator::MATCHSee preg_match.
RegexIterator::REPLACEnone.
RegexIterator::SPLITSee preg_split.

错误/异常

Throws an InvalidArgumentException if the regex argument is invalid.

范例

示例 #1 RegexIterator::__construct example

Creates a new RegexIterator that filters all strings that start with 'test'.

<?php
$arrayIterator = new ArrayIterator(array('test 1', 'another test', 'test 123'));
$regexIterator = new RegexIterator($arrayIterator, '/^test/');

foreach ($regexIterator as $value) {
    echo $value . "\n";
}
?>

以上例程的输出类似于:

test 1
test 123

参见

  • preg_match
  • preg_match_all
  • preg_replace
  • preg_split

RegexIterator::getFlags

Get flags

说明

public int RegexIterator::getFlags ( void )

Returns the flags, see RegexIterator::setFlags for a list of available flags.

返回值

Returns the set flags.

范例

示例 #1 RegexIterator::getFlags example

<?php

$test = array ('str1' => 'test 1', 'teststr2' => 'another test', 'str3' => 'test 123');

$arrayIterator = new ArrayIterator($test);
$regexIterator = new RegexIterator($arrayIterator, '/^test/');
$regexIterator->setFlags(RegexIterator::USE_KEY);

if ($regexIterator->getFlags() & RegexIterator::USE_KEY) {
    echo 'Filtering based on the array keys.';
} else {
    echo 'Filtering based on the array values.';
}
?>

以上例程会输出:

Filtering based on the array keys.

参见

  • RegexIterator::setFlags

RegexIterator::getMode

Returns operation mode

说明

public int RegexIterator::getMode ( void )

Returns the operation mode, see RegexIterator::setMode for the list of operation modes.

返回值

Returns the operation mode.

范例

示例 #1 RegexIterator::getMode example

<?php

$test = array ('str1' => 'test 1', 'teststr2' => 'another test', 'str3' => 'test 123');

$arrayIterator = new ArrayIterator($test);
$regexIterator = new RegexIterator($arrayIterator, '/^[a-z]+/', RegexIterator::GET_MATCH);

$mode = $regexIterator->getMode();
if ($mode & RegexIterator::GET_MATCH) {
    echo 'Getting the match for each item.';
} elseif ($mode & RegexIterator::ALL_MATCHES) {
    echo 'Getting all matches for each item.';
} elseif ($mode & RegexIterator::MATCH) {
    echo 'Getting each item if it matches.';
} elseif ($mode & RegexIterator::SPLIT) {
    echo 'Getting split pieces of each.';
}
?>

以上例程会输出:

Getting the match for each item.

参见

  • RegexIterator::setMode

RegexIterator::getPregFlags

Returns the regular expression flags

说明

public int RegexIterator::getPregFlags ( void )

Returns the regular expression flags, see RegexIterator::__construct for the list of flags.

返回值

Returns a bitmask of the regular expression flags.

范例

示例 #1 RegexIterator::getPregFlags example

<?php

$test = array ('str1' => 'test 1', 'teststr2' => 'another test', 'str3' => 'test 123');

$arrayIterator = new ArrayIterator($test);
$regexIterator = new RegexIterator($arrayIterator, '/\s/', RegexIterator::SPLIT);
$regexIterator->setPregFlags(PREG_SPLIT_NO_EMPTY | PREG_SPLIT_OFFSET_CAPTURE);

if ($regexIterator->getPregFlags() & PREG_SPLIT_NO_EMPTY) {
    echo 'Ignoring empty pieces';
} else {
    echo 'Not ignoring empty pieces';
}

?>

以上例程会输出:

Ignoring empty pieces

参见

  • RegexIterator::setPregFlags

RegexIterator::getRegex

Returns current regular expression

说明

public string RegexIterator::getRegex ( void )

Warning

本函数还未编写文档,仅有参数列表。

参数

此函数没有参数。

返回值

RegexIterator::setFlags

Sets the flags

说明

public void RegexIterator::setFlags ( int $flags )

Sets the flags.

参数

flags
The flags to set, a bitmask of class constants.

The available flags are listed below. The actual meanings of these flags are described in the predefined constants.

valueconstant
1RegexIterator::USE_KEY

返回值

没有返回值。

范例

示例 #1 RegexIterator::setFlags example

Creates a new RegexIterator that filters all entries whose key starts with 'test'.

<?php
$test = array ('str1' => 'test 1', 'teststr2' => 'another test', 'str3' => 'test 123');

$arrayIterator = new ArrayIterator($test);
$regexIterator = new RegexIterator($arrayIterator, '/^test/');
$regexIterator->setFlags(RegexIterator::USE_KEY);

foreach ($regexIterator as $key => $value) {
    echo $key . ' => ' . $value . "\n";
}
?>

以上例程会输出:

teststr2 => another test

参见

  • RegexIterator::getFlags

RegexIterator::setMode

Sets the operation mode

说明

public void RegexIterator::setMode ( int $mode )

Sets the operation mode.

参数

mode
The operation mode.

The available modes are listed below. The actual meanings of these modes are described in the predefined constants.

valueconstant
0RegexIterator::MATCH
1RegexIterator::GET_MATCH
2RegexIterator::ALL_MATCHES
3RegexIterator::SPLIT
4RegexIterator::REPLACE

返回值

没有返回值。

范例

示例 #1 RegexIterator::setMode example

<?php
$test = array ('str1' => 'test 1', 'test str2' => 'another test', 'str3' => 'test 123');

$arrayIterator = new ArrayIterator($test);
// Filter everything that starts with 'test ' followed by one or more numbers.
$regexIterator = new RegexIterator($arrayIterator, '/^test (\d+)/');
// Operation mode: Replace actual value with the matches
$regexIterator->setMode(RegexIterator::GET_MATCH);

foreach ($regexIterator as $key => $value) {
    // print out the matched number(s)
    echo $key . ' => ' . $value[1] . PHP_EOL;
}
?>

以上例程的输出类似于:

str1 => 1
str3 => 123

参见

  • RegexIterator::getMode

RegexIterator::setPregFlags

Sets the regular expression flags

说明

public void RegexIterator::setPregFlags ( int $preg_flags )

Sets the regular expression flags.

参数

preg_flags
The regular expression flags. See RegexIterator::__construct for an overview of available flags.

返回值

没有返回值。

范例

示例 #1 RegexIterator::setPregFlags example

Creates a new RegexIterator that filters all entries with where the array key starts with 'test'.

<?php
$test = array ('test 1', 'another test', 'test 123');

$arrayIterator = new ArrayIterator($test);
$regexIterator = new RegexIterator($arrayIterator, '/^test/', RegexIterator::GET_MATCH);

$regexIterator->setPregFlags(PREG_OFFSET_CAPTURE);

foreach ($regexIterator as $key => $value) {
    var_dump($value);
}
?>

以上例程的输出类似于:

array(1) {
  [0]=>
  array(2) {
    [0]=>
    string(4) "test"
    [1]=>
    int(0)
  }
}
array(1) {
  [0]=>
  array(2) {
    [0]=>
    string(4) "test"
    [1]=>
    int(0)
  }
}

参见

  • RegexIterator::getPregFlags