Home  >  Article  >  Backend Development  >  How to query the key (key name) of an array in php

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

青灯夜游
青灯夜游Original
2022-08-18 19:22:047328browse

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