search
HomeBackend DevelopmentPHP ProblemHow to query in php which element a specified value is in an array

Query steps: 1. Use the array_values() function to reset the array key name and convert the array into an index array, with the syntax "array_values(array)"; 2. Use the array_search() function to obtain the specified value in the index array The position in (index value 1) is enough, the syntax is "array_search("specified value", index array) 1".

How to query in php which element a specified value is in an array

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

In PHP, you can use array_values() and The array_search() function searches for the element in the array that the specified value is.

Implementation steps:

Step 1: Use the array_values() function to convert the array into an index array

array_values() The function returns an array containing all the values ​​in the array.

Simply put, this function can reset the array key name and convert the key name to an index value starting from 0.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$arr=array("a"=>"aa","b"=>"bb","c"=>"cc","d"=>"dd","e"=>"ee");
var_dump($arr);
$values=array_values($arr);
var_dump($values);
?>

How to query in php which element a specified value is in an array

Step 2: Use the array_search() function to obtain the position of the specified value in the index array

array_search() function search Specify the key value and return the corresponding key name, that is, return the corresponding index value.

Because the index value starts counting from 0, the difference from the exact position value is 1; so set the index value to 1 to query the number of the specified value in the array. element.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$arr=array("a"=>"aa","b"=>"bb","c"=>"cc","d"=>"dd","e"=>"ee");
var_dump($arr);
$values=array_values($arr);
var_dump($values);
$index=array_search("bb",$values,true)+1;
echo "指定值是数组中的第 $index 个元素";
?>

How to query in php which element a specified value is in an array

Encapsulate the key code:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;); 
function f($a,$s){
	$v=array_values($a);
	$index=array_search($s,$v,true)+1;
	echo "指定值 $s 是数组中的第 $index 个元素<br>";
}

  
$arr=array("a"=>"aa","b"=>"bb","c"=>"cc","d"=>"dd","e"=>"ee");
var_dump($arr);
f($arr,"bb");
f($arr,"aa");
f($arr,"dd");
?>

How to query in php which element a specified value is in an array

Description:

array_search() function searches for a key value in the array and returns 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.

Recommended study: "PHP Video Tutorial"

The above is the detailed content of How to query in php which element a specified value is in an array. 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 Article

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools