How to get several keys in an array in php

青灯夜游
Release: 2023-03-17 17:42:01
Original
4144 people have browsed it

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.

How to get several keys in an array in php

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)
Copy after login

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));
?>
Copy after login

How to get several keys in an array in php

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));
?>
Copy after login

How to get several keys in an array in php

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 )
Copy after login

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));
?>
Copy after login

How to get several keys in an array in php

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)
Copy after login

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));
?>
Copy after login

How to get several keys in an array in php

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(
    &#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);  // 将数组内部指针向后移动一位
}
?>
Copy after login

How to get several keys in an array in php

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!