hansford Posted March 29, 2011 Share Posted March 29, 2011 Given the following example code; if a function were passed the value of $id is there a way to point to the previous or next element in the array to retrieve their values. thanks $arr = array(); for( $i = 0; $i < 10; $i++ ) { $id = mt_rand() + $i; $arr[$id] = "some value"; } function getValue( $id ) { $element = $arr[$id]; //point to previous element or next element beginning from $element position } Quote Link to comment https://forums.phpfreaks.com/topic/232045-point-to-next-array-element/ Share on other sites More sharing options...
hansford Posted March 29, 2011 Author Share Posted March 29, 2011 I figured it out by trial and error. Here is what I did. function getValue( $id ) { $element = $arr[$id]; --$arg[$id]; extract( $arg ); $element = current( $arg ); extract( $element ); echo $id; //displays previous elements id; $element = next( $arg ); extract( $element ); return $id; } Quote Link to comment https://forums.phpfreaks.com/topic/232045-point-to-next-array-element/#findComment-1193639 Share on other sites More sharing options...
kenrbnsn Posted March 29, 2011 Share Posted March 29, 2011 As written, that function can't work. The arrays $arr & $arg would not be defined inside the function. If you're going to ask a question like this, post the real code. Ken Quote Link to comment https://forums.phpfreaks.com/topic/232045-point-to-next-array-element/#findComment-1193677 Share on other sites More sharing options...
sasa Posted March 29, 2011 Share Posted March 29, 2011 look next() function http://php.net/manual/en/function.next.php Quote Link to comment https://forums.phpfreaks.com/topic/232045-point-to-next-array-element/#findComment-1193754 Share on other sites More sharing options...
hansford Posted March 29, 2011 Author Share Posted March 29, 2011 Sorry guys, here's the exact code; function showValue( $id ) { global $arg; --$arg[$id]; extract( $arg ); $element = current( $arg ); extract( $element ); $element = next( $arg ); echo $id . '<br />'; //displays previous elements id; extract( $element ); echo $id . '<br />'; //displays next elements id; } Quote Link to comment https://forums.phpfreaks.com/topic/232045-point-to-next-array-element/#findComment-1193939 Share on other sites More sharing options...
sasa Posted March 30, 2011 Share Posted March 30, 2011 try <?php $arr = array(); for ($i = 0; $i < 10; $i++) { $id = mt_rand() + $i; $arr[$id] = "some value $i"; } echo '<pre>', print_r($arr), "</pre>\n"; $id = array_rand($arr); echo "Srch element with key $id and value {$arr[$id]}<hr />\n"; function getValue($id, $arr) { end($arr); if ($id == key($arr)) $is_last = true; reset($arr); if ($id == key($arr)) $is_first = true; while (key($arr) != $id) next($arr); if ($is_last) echo "No next elemenrt.<br />\n"; else { next($arr); echo "Next element with key ", key($arr), " is '", current($arr), "'<br />\n"; prev($arr); } if($is_first) echo "No prev element"; prev($arr); echo "Prev element with key ", key($arr), " is '", current($arr), "'<br />\n"; } getValue($id, $arr); ?> Quote Link to comment https://forums.phpfreaks.com/topic/232045-point-to-next-array-element/#findComment-1194123 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.