php8.5
Home/ Manual/ spl / arrayiterator/ ArrayIterator::valid

ArrayIterator::valid

PHP function Edit on GitHub ✎

(PHP 5, PHP 7, PHP 8)

Check whether array contains more entries

Description

ArrayIterator::valid(): bool

Checks if the array contains any more entries.

Parameters Parameters

Return Values

Returns true if the iterator is valid, otherwise false

Examples

ArrayIterator::valid() example

php
<?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());
?>

Source: reference/spl/arrayiterator/valid.xml · from the official PHP manual (php/doc-en)