How to find the largest and smallest 10 numbers in a numerical array in PHP

不言
Release: 2023-04-01 16:20:01
Original
1781 people have browsed it

This article mainly introduces the method of PHP to find the largest and smallest 10 numbers in a numerical array, involving the usage skills of array_unique and array_slice methods in PHP. It is of great practical value. Friends who need it can refer to it

The example of this article describes the method of PHP to find the 10 largest and smallest numbers in a numerical array that do not repeat. Share it with everyone for your reference. The details are as follows:

1. The php code is as follows:

//随机生成1万个元素的数组
for($i=0;$i<10000;$i++){
 $ary[]=rand(1,100000);
}
$ary=array_unique($ary); //去重复数值
sort($ary);//顺序排序
$min_10=array_slice($ary,0, 10);//取出最小的10个数值
$max_10=array_slice($ary,-10, 10);//取出最大的10个数值
rsort($max_10);//倒序排序最大的10个数值
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($min_10);
print_r($max_10);
unset($ary,$min_10,$max_10);
Copy after login

2. The running result is as follows:

Array
(
  [0] => 16
  [1] => 19
  [2] => 22
  [3] => 31
  [4] => 40
  [5] => 49
  [6] => 71
  [7] => 74
  [8] => 80
  [9] => 92
)
Array
(
  [0] => 99997
  [1] => 99991
  [2] => 99973
  [3] => 99958
  [4] => 99955
  [5] => 99946
  [6] => 99939
  [7] => 99933
  [8] => 99927
  [9] => 99900
)
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About the function code for finding polynomial derivatives in PHP

How to use PHP to view the current variable type

The above is the detailed content of How to find the largest and smallest 10 numbers in a numerical 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!