Jump to content


Array Seek Function [Solved]


  • You cannot reply to this topic
No replies to this topic

#1 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 972 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 12 March 2007 - 05:06 PM

Edit:
Who would have guessed it, 10 minutes after posting this, I finally find a function using Google, lol.
Only thing is that it wasn't call anything like what I would imagine it being called, lol.

Here is the source I found it for anyone who might need.
http://algorytmy.pl/...ion.current.php
(First User Note)



Hey guys, I need some help with developing a function, and I just can't figure how to make it.

Here's the scenario.

I have an array that is being iterated through a foreach loop. I need to be able to preview the element that is next in the loop, in order to be able tell it if the script needs to close off an HTML element. Now, when I use a peek() function I found on PHP, it doesn't return it quite right, because the array apparently isn't passed as a reference to the foreach block.

Here are some tests I've run.

// Peek at Next Array Value
function peek(&$array){
	$ret = next($array);
	prev($array);
	return $ret;
}

$array = array(1, 2, 3);

foreach($array as $v){
	echo current($array).'<br />';
}
// Echos 1 three times


echo 'Start: '.current($array).'<br />'; // 1
next($array);
echo 'Now at: '.current($array).'<br />'; // 2
echo 'Peeking at next value: '.peek($array).'<br />'; // 3
echo 'And the current value is: '.current($array); // 2

I've not found any way to pass a reference of the actual array to the foreach, only a reference to the value. I was going to try to develop a function to seek the internal pointer to a certain key of a value, and return the array as a reference. This way, I could make a temporary array as a copy of the original array, then seek its value using the foreach's '$array as $k => $v' method, then could simply use that with the peek() function.

I've tried something like this.

function &array_seek(&$array, $key){
	$temp_array = $array;
	while(($next = next($temp_array)) !== $key && $next !== false)
		next($array);
	return $array;
}

echo 'Start: '.current($array).'<br />'; // 'one'
next($array); 
echo 'Now at: '.current($array).'<br />'; // 'two'
echo 'Peeking at next value: '.peek($array).'<br />'; // 'three'
echo 'And the current value is: '.current($array).'<br />'; // 'two'
$array = array_seek($array, 'key');
echo 'Seek test: '.current($array).'<br />'; // Gives a blank, because current() is returning FALSE
echo 'Peek again: '.peek($array); // And another blank

I've tried alot of stuff, and it's baffling me why it's not returning the array with the pointer correctly.

Any ideas? Or is there any way to do this with the each() function or some other method?

Edited by Demonslay, 12 March 2007 - 05:23 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users