note 39062 added to function.next

From: Date: Thu, 15 Jan 2004 18:06:07 +0000
Subject: note 39062 added to function.next
Groups: php.notes 
Request: Send a blank email to php-notes+get-63639@lists.php.net to get a copy of this message
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' ---- Manual Page -- http://www.php.net/manual/en/function.next.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+39062 Delete -- http://master.php.net/manage/user-notes.php?action=delete+39062&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+39062&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#63639) next »