Hello, I got an issue, i want to output the next KEY from the array "array" but the next code is outputing "array" and obvious not the next key(which is 6) <?php $array = array( 9 => 'test', 5 => 'asdasd', 6 => array('fff','sdsdsd','sdsdsd') ); function get_next($array, $key) { $currentKey = key($array); while ($currentKey !== null && $currentKey != $key) { next($array); $currentKey = key($array); } return next($array); } echo get_next($array, '5');?> Code (markup):
The next() function advances the pointer of an array and returns the value of the next element. So what you're doing is breaking out of the loop when the array key matches your $key parameter (i.e. when the array key is 5), and you're then calling next() one more time (which advances the pointer on to the next element) and returning its value (which is the value of the array element, not the key). If you want to return the key, you'll have to advance the pointer and then return key($array). Replacing your return statement with the following should work: ​next($array); return key($array); PHP:
Thanks mate, that worked, but i encountered another issue... when im getting to the last element of the array, it doesent get back to the first... and i cant imagine how to do that since the function end($array) will positionate the current key as the last... i dont know how to test if the current element s the last
next() will return false if it can no longer advance the internal pointer (i.e. if it's reached the end of the array). So you'll want to see if it's false, and if so, reset the pointer. I'm not sure why you want to reset the pointer (I can only assume you want to return the key for the first element if either a) You send through the key to the last element or, b) The key wasn't found). In either case, you could do something like: next($array) === false && reset($array); return key($array); PHP: It's basically checking to see if we've reached the end of our array and, if so, resets it.
Hello, I've done a little different, i identified the last array's element, and assigned the key to $lastKey, and after that i tested with an if to see if the current key is the last key function get_next($array, $key) { end($array); $lastKey = key($array); reset($array); $currentKey = key($array); while ($currentKey !== null && $currentKey != $key) { next($array); $currentKey = key($array); } if($lastKey == $currentKey){ reset($array); }else{ next($array); } Code (markup): Thanks for your help, you rock!!!
That works as well, if a little convoluted You could always shorten it down to something like: function get_next($array, $key) { while (key($array) != $key && next($array) !== false); next($array) === false && reset($array); return key($array); } PHP:
how would this be translated? while (key($array) != $key && next($array) !== false); Code (markup): im new to php, and im not used with this syntax Thanks
Uh, it's essentially a while loop that doesn't execute any code between iterations. You can think of it like: while (key($array) != $key && next($array) !== false) { // We're doing nothing } PHP: Essentially the first part (key($array) != $key) is checking if the key for the current array element matches the key we passed through (and if it does the loop will be terminated). The second part (next($array) !== false) is basically checking if we've reached the end of our array. So essentially, we'll break out of the loop if 1) We've found the key or 2) We've reached the end. Also, because we're calling the next() function within our condition, we're advancing the pointer by one each iteration.