Go to the next key in the array

Suppose I know that there is a key "twoVal", but I do not know what is after it. How can I go to the next key? Shoud I know the position of the key two-shaft? Or is there another way?

$arr = array('Cool Viski' => array('oneVal' => '169304', 'twoVal' => '166678', 'threeVal' => '45134')); 
+6
php
source share
3 answers
 $keys = array_keys($arr['Cool Viski']); $position = array_search('twoVal', $keys); if (isset($keys[$position + 1])) { $keyAfterTwoVal = $keys[$position + 1]; } 
+17
source share
 $arr = array('Cool Viski' => array('oneVal' => '169304', 'twoVal' => '166678', 'threeVal' => '45134')); foreach($arr as $s=>$v){ foreach($v as $val){ if(key($v) == "twoVal"){ $t=next($v); print "next key: ".key($v)."\n"; print "next key value is: ".$t."\n";; }else{ next($v); } } } 
+1
source share

You may be interested in various array search functions , but , unless the PHP array is indexed only integers do not guarantee the order on the keys.

0
source share

All Articles