search
HomeBackend DevelopmentPHP ProblemHow to query the key (key name) of an array in php

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)".

How to query the key (key name) of an array in php

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(
    &#39;name&#39; => &#39;中文网&#39;,
    &#39;url&#39; => &#39;http&#39;,
    &#39;age&#39; => 8,
    &#39;desc&#39; => &#39;一个学习编程的网站&#39;,
    &#39;course&#39; => &#39;PHP教程&#39;
);
for ($i=0,$len=count($info); $i<$len; $i++) {
    echo key($info) . "<br/>";  //输出内部指针指向的当前元素的键
    next($info);  // 将数组内部指针向后移动一位
}
?>

How to query the key (key name) of an array in php

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));
?>

How to query the key (key name) of an array in php

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));
?>

How to query the key (key name) of an array in php

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:
  • true
  • false - default
If set to true, the type of the given value in the array is checked, the number 5 and the string 5 are different (see Example 2).
  • 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(&#39;content-type:text/html;charset=utf-8&#39;);   
$arr=array("id"=>1,"name"=>"李华","age"=>23);
var_dump($arr);
echo "指定值&#39;李华&#39;对应的键名为:".array_search("李华",$arr);
?>

How to query the key (key name) of an array in php

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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

MinGW - Minimalist GNU for Windows

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

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

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

SublimeText3 English version

Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft