Home>Article>Backend Development> 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

青灯夜游
青灯夜游 Original
2022-08-23 19:47:55 2981browse

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.

"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 theindex value to 1to query the number of the specified value in the array. element.

"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:

"; } $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