Home > Article > Backend Development > How to find the smallest elements of an array in php
Implementation steps: 1. Use the asort() function to sort the array in ascending order by key value. The syntax is "asort (original array)". The smallest elements after sorting are concentrated at the beginning of the array; 2. Use array_slice The () function can intercept N minimum elements from the beginning of the array, the syntax is "array_slice (sorted array, 0, N)".
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
In PHP, you can use the asort() function and array_slice() function to find the smallest N elements of the array.
Implementation idea:
Reorder the array and sort the array elements in ascending order from small to large. (In this way, the smallest element is at the beginning of the array)
Just intercept N array elements directly from the array kait.
Implementation steps:
1. Use the asort() function to sort the array in ascending order by key value Sorting
<?php header('content-type:text/html;charset=utf-8'); $arr=array(34,3,-1,-6,42,12,1); echo "原数组:"; var_dump($arr); asort($arr); echo "升序排序后:"; var_dump($arr); ?>
You can see that after sorting, the smallest element is at the beginning of the array, and you only need to intercept the specified number as needed.
Step 2: Use the array_slice() function to intercept N elements from the beginning of the array
The array_slice() function is a function provided by PHP for intercepting arrays
If you want to intercept N elements from the beginning of the array, you only need to set the second parameter to 0 and the third parameter to the number of elements N.
array_slice(排序后数组,0,N)
Example: Get the smallest 2, 3, and 4 elements of the array
<?php header('content-type:text/html;charset=utf-8'); $arr=array(34,3,-1,-6,42,12,1); echo "原数组:"; var_dump($arr); asort($arr); echo "升序排序后:"; var_dump($arr); echo "获取数组最小的2个元素:"; $result = array_slice($arr,0,2); //截取数组前2位的元素 var_dump($result); echo "获取数组最小的3个元素:"; $result = array_slice($arr,0,3); //截取数组前3位的元素 var_dump($result); echo "获取数组最小的4个元素:"; $result = array_slice($arr,0,4); //截取数组前3位的元素 var_dump($result); ?>
Extended knowledge: Function introduction
1. asort() function
asort() function will sort the associative array in ascending order according to the key value. And the key names in the original array will not be modified.
asort($array,$sortingtype)
asort() function has two parameters: $array (required) and $sortingtype (can be omitted).
Among them, the $sortingtype parameter is used to define the function sorting mode and specify how to compare the elements/items of the array. The default value is "SORT_REGULAR".
The $sortingtype parameter can be set to the following values:
0 = SORT_REGULAR: Compare array elements normally without changing their type (default value);
1 = SORT_NUMERIC: Treat array elements as numbers;
2. array_slice() function
array_slice() function can extract a fragment from the arrayarray array_slice ( array $arr , int $start [, int $length = NULL [, bool $preserve_keys = false ]] )Parameters Description:
<?php header("Content-type:text/html;charset=utf-8"); $arr = array(10,12,20,25,24); echo "原数组:"; var_dump($arr); echo "截取的数组片段:"; $result = array_slice($arr,2); var_dump($result); $result = array_slice($arr,-2); var_dump($result); ?>Example 2 : Parameter $length
<?php header("Content-type:text/html;charset=utf-8"); $arr = array(10,12,20,25,24); echo "原数组:"; var_dump($arr); echo "截取的数组片段:"; $result = array_slice($arr,1,1); var_dump($result); $result = array_slice($arr,1,-1); var_dump($result); ?>Example 3: Parameter $preserve indicates whether to retain the original key name. The default value is false, that is, not retained; If set to true, the original key name will be retained.
<?php header("Content-type:text/html;charset=utf-8"); $arr = array(10,12,20,25,24); echo "原数组:"; var_dump($arr); echo "截取的数组片段:"; $result = array_slice($arr,1,1,true); var_dump($result); $result = array_slice($arr,1,-1,true); var_dump($result); ?>Recommended learning: "
PHP Video Tutorial"
The above is the detailed content of How to find the smallest elements of an array in php. For more information, please follow other related articles on the PHP Chinese website!