tags:

views:

60

answers:

3

Let's assume I know that there is key "twoVal", but I do not know what is after it. How do I get to the next key for that matter? Shoud I know the position of key "twoVal"? Or there is another way around?

$arr = array('Cool Viski' => array('oneVal' => '169304',
                                   'twoVal' => '166678',
                                   'threeVal' => '45134'));
A: 

You might be interested in the various array seeking functions, but unless a PHP array is indexed only by integers there's no guarantee of order on the keys.

Alan Storm
+4  A: 
$keys = array_keys($arr['Cool Viski']);
$position = array_search('twoVal', $keys);
if (isset($keys[$position + 1])) {
    $keyAfterTwoVal = $keys[$position + 1];
}
deceze
That's intelligent!
Nirmal
I think it'd be more intelligent to do whatever the OP is trying to do another way than nitty gritty array manipulation, but it answers the question. :)
deceze
A: 
$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);
        }
    }
}
ghostdog74