DirectoryIterator::key

Return the key for the current DirectoryIterator item

&reftitle.description; public `mixed` `DirectoryIterator::key`

Get the key for the current `DirectoryIterator` item.

&reftitle.parameters; &no.function.parameters;
&reftitle.returnvalues;

The key for the current `DirectoryIterator` item as an &integer;.

&reftitle.changelog;

&Version; | &Description;
8.1.0 | When the iterator is uninitialized, an `Error` is thrown now. Previously, the method returned &false;.

&reftitle.examples;

### A `DirectoryIterator::key` example

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

&example.outputs.similar;

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

&reftitle.seealso;

- `DirectoryIterator::current`
- `DirectoryIterator::next`
- `DirectoryIterator::rewind`
- `DirectoryIterator::valid`
- `Iterator::key`