我们假设我知道有关键的“twoVal”,但我不知道它后面是什么。我如何获得下一个关键的问题?我知道关键“twoVal”的位置吗?还是有另一种方式?
$arr = array('Cool Viski' => array('oneVal' => '169304',
'twoVal' => '166678',
'threeVal' => '45134'));
答案 0 :(得分:17)
$keys = array_keys($arr['Cool Viski']);
$position = array_search('twoVal', $keys);
if (isset($keys[$position + 1])) {
$keyAfterTwoVal = $keys[$position + 1];
}
答案 1 :(得分:1)
$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);
}
}
}
答案 2 :(得分:-1)
您可能对各种array seeking functions,但感兴趣,除非PHP数组仅按整数编制索引,否则无法保证按键顺序。