Home > Backend Development > PHP Problem > How to find the smallest element value and subscript in an array in php

How to find the smallest element value and subscript in an array in php

青灯夜游
Release: 2023-03-16 21:40:02
Original
3272 people have browsed it

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)".

How to find the smallest element value and subscript in an array in php

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(&#39;content-type:text/html;charset=utf-8&#39;);   
$arr=array(1,45,9,0,-5,52,21,-1,40);
var_dump($arr);
$min=min($arr);
echo "数组最小值为: ".$min."<br>";
?>
Copy after login

How to find the smallest element value and subscript in an array in php

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

How to find the smallest element value and subscript in an array in php

Encapsulate the function into a function:

function f($arr){
	$min=min($arr);
	echo "数组最小值为: ".$min."<br>";
	$index=array_search($min,$arr);
	echo "最小值的下标为:".$index;
}
Copy after login

Just call f($arr):

$arr=array(1,2,3);
var_dump($arr);
f($arr);

$arr=array(1,0,1);
var_dump($arr);
f($arr);
Copy after login

How to find the smallest element value and subscript in an array in php


## 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,...);
Copy after login

ParametersDescriptionRequired . Specifies an array containing values. Required. Specifies the values ​​to be compared (at least two values).
array_values
value1,value2,...
Return value: minimum numerical value.​

Note: If you want to request the maximum value, you can use the max() function.

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)
Copy after login
ParametersDescriptionRequired . Specifies the key value to search for in the array. Required. Specifies the array to be searched. Optional. If this parameter is set to TRUE, the function searches the array for elements of the same data type and value. Possible values:
value
array
strict
    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 learning: "

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!

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