Check whether array contains more entries with ArrayIterator::valid in PHP

Description

The following code shows how to check whether array contains more entries with ArrayIterator::valid.

Example


//  ww  w.j av  a 2  s .  c o  m
<?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

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

The code above generates the following result.





















Home »
  PHP Tutorial »
    Data Types »




Array
Array Associative
Array Util
ArrayObject
Data Types
Date
Date Format
DateTime
Number
String
String Escape
String Filter
String HTML
String Type
Timezone