简体   繁体   中英

How to get the key of the current item of an array?

使用next()prev()导航数组时,如何获取数组所在的当前键?

You can use the key function :

key() returns the index element of the current array position.

And, as a quick example, you can consider this portion of code :

$array = array(
    'first' => 123,
    'second' => 456,
    'last' => 789, 
);

reset($array);      // Place pointer on the first element
next($array);       // Advance to the second one
$key = key($array); // Get the key of the current (i.e. second) element

var_dump($key);

It'll output, as expected, the key of the second element :

string 'second' (length=6)

使用key函数获取内部指针当前指向的项的键。

你可能想要key()

you can get this in very simple way that is

foreach( $array as $key=>$value)
{   
   echo " Key ". $key;
}

It will echo last key name .. you can manage after that as you want to do. \\ My solution is for get current key name of Array Index.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM