Home > Backend Development > PHP Problem > How to query in php which element a specified value is in an array

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

青灯夜游
Release: 2023-03-16 18:44:02
Original
3086 people have browsed it

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

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 个元素";
?>
Copy after login

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

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

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