note 73138 deleted from ref.array by felipe

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
Note Submitter: Colin 

----

I couldn't get the previous set pointer functions working for whatever reason so I modifed. I trigger errors if needed but obviously you can do whatever

	// setPointer - Sets the pointer to the provided $key in provided $array
	final private function setPointer(&$array, $set_key) {
	
		// Check if $array is an array
		if (!is_array($array)) {
			trigger_error('$array must be an array', E_USER_ERROR);
			}
	
		// Check if $key is in $array
		if (!array_key_exists($set_key, $array)) {
			trigger_error('$set_key must exist in $array', E_USER_ERROR);
			}

		// Set array pointer to first element
		reset($array);
	
		// Cycle through array
		while ($set_key != key($array)) {
		
			// Advance the pointer
			next($array);
			}
		}

Try with:
$array = array();
$array[0] = 'zero';
$array[1] = 'one';
$array[2] = 'two';
$array[3] = 'three';

// Set the pointer
setPointer($array, 1);

// Display elements
echo current($array); // Outputs 'one'
echo next($array); // Outputs 'two'
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.