note 39062 deleted from function.next by aidan
| From: | aidan@php.net | Date: | Sat, 18 Sep 2004 04:30:55 +0000 |
| Subject: | note 39062 deleted from function.next by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-76903@lists.php.net to get a copy of this message | ||
Note Submitter: webmaster at php-projekte dot de
----
here is a java like implementation of getElement( int index, array &array ) using next. it can
be useful if you want to move the array's pointer to desired position+1 AND return a value:
function getElement( $which, $arr )
{
if( count( $arr ) == 0 )
{
die( "EmptyArrayException in getElement()!" );
}
else if( $which >= count( $arr ) || $which < 0 )
{
die( "IndexOutOfBoundsException in getElement()!" );
}
$pos = 0;
$curr = current( &$arr );
while( hasMoreElements( &$arr ) )
{
if( $pos == $which )
{
return $curr;
}
$curr = current( &$arr );
$pos++;
}
return $curr;
}
function hasMoreElements( $arr )
{
if( next( &$arr ) )
{
return TRUE;
}
else
{
return FALSE;
}
}
$arr = array( "peter", "paul", "marry" );
echo getElement( 2, &$arr ); // prints 'marry'