note 42545 added to function.next
| From: | courtshrock at rn2 dot php dot net | Date: | Thu, 20 May 2004 20:36:05 +0000 |
| Subject: | note 42545 added to function.next | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-69886@lists.php.net to get a copy of this message | ||
This code returns neighbors of the specified key. The result will be empty if it doesn't have
any neighbors. My approach was to use the order of keys to determine neighbors, which is differnet
from just getting the next/previous element in an array. Feel free to point out stupidities :)
<?php
function array_neighbor($arr, $key)
{
krsort($arr);
$keys = array_keys($arr);
$keyIndexes = array_flip($keys);
$return = array();
if (isset($keys[$keyIndexes[$key]-1]))
$return[] = $keys[$keyIndexes[$key]-1];
if (isset($keys[$keyIndexes[$key]+1]))
$return[] = $keys[$keyIndexes[$key]+1];
return $return;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.next.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+42545
Delete -- http://master.php.net/manage/user-notes.php?action=delete+42545&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+42545&report=yes
Search -- http://master.php.net/manage/user-notes.php