note 44869 added to function.next
| From: | lukasz at karapuda dot com | Date: | Wed, 18 Aug 2004 17:06:17 +0000 |
| Subject: | note 44869 added to function.next | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-74887@lists.php.net to get a copy of this message | ||
This function will return the previous,next neighbors of an array entry within an associative array.
If the specified $key points to the last or first element of the array, the first or last keys of
the array will be returned consecutively. This is an improved version of the same function posted
earlier.
<?php
function array_neighbor($arr, $key)
{
$keys = array_keys($arr);
$keyIndexes = array_flip($keys);
$return = array();
if (isset($keys[$keyIndexes[$key]-1])) {
$return[] = $keys[$keyIndexes[$key]-1];
}
else {
$return[] = $keys[sizeof($keys)-1];
}
if (isset($keys[$keyIndexes[$key]+1])) {
$return[] = $keys[$keyIndexes[$key]+1];
}
else {
$return[] = $keys[0];
}
return $return;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.next.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+44869
Delete -- http://master.php.net/manage/user-notes.php?action=delete+44869&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+44869&report=yes
Search -- http://master.php.net/manage/user-notes.php