note 106328 added to language.types.array
| From: | Anonymous at osu1 dot php dot net | Date: | Fri, 28 Oct 2011 00:00:00 +0000 |
| Subject: | note 106328 added to language.types.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-184188@lists.php.net to get a copy of this message | ||
This is a simple solution for taking previous and next items, even if we are at the ends of the
array.
<?php
$current_key; // the key of the item we want to search from
if (isset($array[$current_key+1])) {
$array_next = $array[$current_key+1]; // get the next item if there is
} else {
$array_next = $array[0]; // if not take the first (this means this is the end
of the array)
}
if (isset($array[$current_key-1])) {
$array_prev = $array[$current_key-1]; // get the previous item if there is
} else {
$array_prev = $array[count($array)-1]; // if not take the last item (this means this is the
beginning of the array)
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 87.126.0.28
----
Manual Page -- http://www.php.net/manual/en/language.types.array.php
Edit -- https://master.php.net/note/edit/106328
Del: integrated -- https://master.php.net/note/delete/106328/integrated
Del: useless -- https://master.php.net/note/delete/106328/useless
Del: bad code -- https://master.php.net/note/delete/106328/bad+code
Del: spam -- https://master.php.net/note/delete/106328/spam
Del: non-english -- https://master.php.net/note/delete/106328/non-english
Del: in docs -- https://master.php.net/note/delete/106328/in+docs
Del: other reasons-- https://master.php.net/note/delete/106328
Reject -- https://master.php.net/note/reject/106328
Search -- https://master.php.net/manage/user-notes.php