Implementation steps: 1. Use the min() function to obtain the smallest element value in the array, the syntax "$min=min($arr);"; 2. Use the array_search() function to search for the obtained value in the array The minimum value will return the corresponding subscript value, the syntax is "array_search($min,$arr)".
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer
Analysis: PHP to find the minimum in an array The element values and subscripts can be divided into two steps
Get the minimum element value (minimum value) of the array
According to the minimum Value to find the corresponding subscript (key name) of the value in the array
The following will give you a detailed introduction to the implementation steps.
Step 1: Get the minimum value of the array
PHP has many ways to get the maximum value of the array, such as sorting (ascending or descending order), so that the elements at the beginning or end It's the best value. [Related recommendation: How to find the minimum value in an array in php]
But because you still need to get its subscript, sorting is not necessarily optional (it may destroy its original order) .
Then we can just use the built-in function --min().
<?php header('content-type:text/html;charset=utf-8'); $arr=array(1,45,9,0,-5,52,21,-1,40); var_dump($arr); $min=min($arr); echo "数组最小值为: ".$min."<br>"; ?>
Step 2: Find the subscript (key name) of the value in the array based on the minimum value
PHP provides A built-in function--array_search()
This function can search for a given value in the array and return the corresponding key name (subscript) if successful.
$index=array_search($min,$arr); echo "最小值的下标为:".$index;
Encapsulate the function into a function:
function f($arr){ $min=min($arr); echo "数组最小值为: ".$min."<br>"; $index=array_search($min,$arr); echo "最小值的下标为:".$index; }
Just call f($arr):
$arr=array(1,2,3); var_dump($arr); f($arr); $arr=array(1,0,1); var_dump($arr); f($arr);
## Function description:
1. min() function
min() function returns the minimum value in an array, or the minimum value among several specified values.min(array_values); or min(value1,value2,...);
Description | |
---|---|
array_values | Required . Specifies an array containing values.|
value1,value2,... | Required. Specifies the values to be compared (at least two values).
2. array_search() function
array_search() function searches for a key value in the array and returns the corresponding key name.array_search(value,array,strict)
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:
|
PHP Video Tutorial", "PHP ARRAY"
The above is the detailed content of How to find the smallest element value and subscript in an array in php. For more information, please follow other related articles on the PHP Chinese website!