Getting method: 1. Use the array_keys() function to get all the keys. The syntax is "array_keys(array)". You can also get the key with a specified value. The syntax is "array_keys(array, value, whether to use strict mode). )". 2. Use array_key_first() to get the first key, the syntax is "array_key_first(array)". 3. Use array_key_last() to get the last key. 4. Use key() to get the key of the current element.
The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer
php gets the array key (key Name) 4 methods
Method 1: Use array_keys() function to obtain one or more keys
array_key() function can be obtained Some or all key names (subscripts) in the array, the syntax format of this function is as follows:
array_keys($array,$search_value,$strict)
Parameter description is as follows:
===
. array_key() function will return the obtained array key name in the form of an array.
Example 1: All key names
<?php $arr=array("Peter"=>65,"Harry"=>80,"John"=>78,"Clark"=>90); var_dump($arr); var_dump(array_keys($arr)); ?>
Example 2: Key names of specified values
<?php $arr=array("Peter"=>65,"Harry"=>80,"John"=>80,"Clark"=>90); var_dump($arr); var_dump(array_keys($arr,80)); var_dump(array_keys($arr,"80")); var_dump(array_keys($arr,"80",true)); ?>
Method 2: Use the array_key_first() function to obtain the first key of the specified array
array_key_first() function is used to obtain the first key (key) of the specified array , will not affect the internal pointer of the original array.
The syntax format of this function is as follows:
array_key_first ($array )
Return value:
Returns the first key of array (if not empty), otherwise returns null.
Example:
<?php $arr=array("Peter"=>65,"Harry"=>80,"John"=>80,"Clark"=>90); var_dump($arr); var_dump(array_key_first($arr)); ?>
Method 3: Use the array_key_last() function to get the last key of the specified array
array_key_last() function obtains the last key of an array and does not affect the internal pointer of the original array.
array_key_last ($array)
Return value:
Returns the last key of array (if not empty), otherwise returns null.
Example:
<?php $arr=array("Peter"=>65,"Harry"=>80,"John"=>80,"Clark"=>90); var_dump($arr); var_dump(array_key_last($arr)); ?>
Method 4: Use the key() function to get the key of the current element
key() function can return the key name of the element currently pointed by the internal pointer of the array, that is, obtain the key name of the current element in the array.
There is a pointer inside each PHP array, which points to an element of the array. The pointed element is the "current element".
Example:
<?php $info = array( 'name' => '中文网', 'url' => 'http', 'age' => 8, 'desc' => '一个学习编程的网站', 'course' => 'PHP教程' ); for ($i=0,$len=count($info); $i<$len; $i++) { echo key($info) . "<br/>"; //输出内部指针指向的当前元素的键 next($info); // 将数组内部指针向后移动一位 } ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to get several keys in an array in php. For more information, please follow other related articles on the PHP Chinese website!