Array peek

By sean on May 27, 2011

string array_peek( array $a, string $k )
This function returns a string of the next key in sequential order of $a, delimited by $k. Useful when iterating through an array. and needing to "peek ahead" of a key.

$a - The array (haystack)
$k - the key prior to expected result (needle)


$a = array('x' => 1, 'i' => 2, 'g' => 3, 'k' => 4, 'l' => 5);
echo array_peek($a, 'x');

The above example will output:

function array_peek($a, $k) {
  foreach($a as $x => $v) {
    if (isset($h)) return $x;
    if ($x == $k) $h = 1;


Sign in to comment.
Korvin   -  Oct 03, 2011
function array_peek($a,$k) {
    return (array_search($k,$a)!=count($a)-1?$a[array_search($k,$a)+1]:false);

That's how I'd do it =]

sean   -  May 30, 2011

lol yup, how ya been m8?

and thanx :)

F*U*R*B*Y*   -  May 29, 2011

Geez, Sean, your still around???

Nice, Simple and Easy. Great job :)

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.