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.

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

Example:

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

The above example will output:

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

Comments

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

That's how I'd do it =]

 Respond  
sean   -  May 30, 2011

lol yup, how ya been m8?

and thanx :)

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

Geez, Sean, your still around???

Nice, Simple and Easy. Great job :)

 Respond  
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.