How to find array subscript in php

青灯夜游
Release: 2023-03-16 07:52:01
Original
4061 people have browsed it

Two methods: 1. Use array_keys, the syntax "array_keys(array, search value, false|true)" will return an array containing subscripts. 2. Traverse the array and store the subscript into an empty array with the syntax "foreach(array as $k=>$v){$r[]=$k;}".

How to find array subscript in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer

Array array is a set of ordered variables. Each value is called an element. Each element is distinguished by a special identifier called a key (also called a subscript).

So how to find the array subscript in PHP? Two methods are introduced below.

Method 1: Directly use the built-in function array_keys()

array_key() function can get some or all key names (subscripts) in the array, the function syntax The format 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 find array subscript 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));
?>
Copy after login

How to find array subscript in php

Method 2: Use the foreach statement to traverse the array and store the key name into an empty array

<?php
$arr=array("Peter"=>65,"Harry"=>80,"John"=>78,"Clark"=>90);
var_dump($arr);
$r=[];
foreach($arr as $k=>$v){
	$r[]=$k;
}
var_dump($r);
?>
Copy after login

How to find array subscript in php

Recommended study: "PHP video tutorial

The above is the detailed content of How to find array subscript 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