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: required parameter, which is the array to be operated;
- $search_value: Optional parameter. If the parameter is empty, the function will return all the key names in the array. If this parameter is specified, the function will only return the key name with the value $search_value;
-
$strict: Optional parameter to determine whether to use strict mode when searching. $strict defaults to false, which is non-strict mode. Only types are compared during search, not types. If $strict is set to true, that is Strict mode, compares both value and type when searching, equivalent to
===
.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version
Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
