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".
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('content-type:text/html;charset=utf-8'); $arr=array("a"=>"aa","b"=>"bb","c"=>"cc","d"=>"dd","e"=>"ee"); var_dump($arr); $values=array_values($arr); var_dump($values); ?>
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('content-type:text/html;charset=utf-8'); $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 个元素"; ?>
Encapsulate the key code:
<?php header('content-type:text/html;charset=utf-8'); 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"); ?>
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:
|
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!