3 methods: 1. Use the key() function to query the key (key name) of the current array element, with the syntax "key (array)". 2. Using the array_keys() function, you can get all the keys of the array with the syntax "array_keys(array)". You can also get the key with a specified value with the syntax "array_keys(array, value, whether to use strict mode)". 3. Use array_search() to query the key of a specified value, the syntax is "array_search(value, array)".
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
php query array key 3 methods of (key name)
Method 1: Use key() function to query
key() function can return the internal pointer of the array The key name currently pointing to the element, that is, the key name of the current element in the array is obtained.
There is a pointer inside each PHP array, which points to an element of the array. The pointed element is the "current element".
Default
<?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); // 将数组内部指针向后移动一位 } ?>
Method 2: Use the array_keys() function to query
The array_key() function can obtain some or all key names (subscripts) in the array. The syntax format of this function is as follows:
array_keys($array,$search_value,$strict)
The 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"=>78,"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 3: Use array_search() function to query
array_search() function can search for the specified key value in the array and return the corresponding key name.
array_search(value,array,strict)
Parameters | Description |
---|---|
value | Required . Specifies the key value to search for in the array. |
array | Required. Specifies the array to be searched. |
strict | Optional. If this parameter is set to TRUE, the function searches the array for elements of the same data type and value. Possible values:
|
Return value: If the specified key value is found in the array, return the corresponding key name, otherwise return FALSE. If a key value is found more than once in the array, the key name matching the first found key value is returned.
<?php header('content-type:text/html;charset=utf-8'); $arr=array("id"=>1,"name"=>"李华","age"=>23); var_dump($arr); echo "指定值'李华'对应的键名为:".array_search("李华",$arr); ?>
Recommended study: "PHP Video Tutorial"
The above is the detailed content of How to query the key (key name) of an array in php. For more information, please follow other related articles on the PHP Chinese website!