note 99964 deleted from function.array-search by peehaa

From: Date: Fri, 07 Apr 2017 10:36:07 +0000
Subject: note 99964 deleted from function.array-search by peehaa
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-209005@lists.php.net to get a copy of this message
Note Submitter: hanan dot ali dot shaikh at googlemail dot com ---- 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

« previous php.notes (#209005) next »