note 99964 added to function.array-search
| From: | hanan dot ali dot shaikh at googlemail dot com | Date: | Fri, 17 Sep 2010 08:14:53 +0000 |
| Subject: | note 99964 added to function.array-search | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-172667@lists.php.net to get a copy of this message | ||
In this code I write a code to find next and previous element of an array using current element of
that array. Let suppose if we are in element 9 and have to access its next and previous element then
this code be helpful for someone.
<?php
$myArray = array(4,5,7,9,10,11,13,19,25);
$currentElement = 9;
$firstElement = current($myArray);
$lastElement = $myArray[sizeof($myArray)-1];
$currentKey = array_search($currentElement, $myArray);
$currentValue = $myArray[$currentKey];
$previousValue = "";
$nextValue = "";
if($currentElement!=$lastElement){
$nextKey = $currentKey + 1;
$nextValue = $myArray[$nextKey];
}
if($currentElement!=$firstElement){
$previousKey = $currentKey - 1;
$previousValue = $myArray[$previousKey];
}
echo $previousValue."--".$currentValue."--".$nextValue;
?>
Thanks,
Hanan Ali
----
Server IP: 69.147.83.197
Probable Submitter: 124.109.48.3
----
Manual Page -- http://www.php.net/manual/en/function.array-search.php
Edit -- https://master.php.net/note/edit/99964
Del: integrated -- https://master.php.net/note/delete/99964/integrated
Del: useless -- https://master.php.net/note/delete/99964/useless
Del: bad code -- https://master.php.net/note/delete/99964/bad+code
Del: spam -- https://master.php.net/note/delete/99964/spam
Del: non-english -- https://master.php.net/note/delete/99964/non-english
Del: in docs -- https://master.php.net/note/delete/99964/in+docs
Del: other reasons-- https://master.php.net/note/delete/99964
Reject -- https://master.php.net/note/reject/99964
Search -- https://master.php.net/manage/user-notes.php