Home  >  Article  >  Backend Development  >  How to find the smallest elements of an array in php

How to find the smallest elements of an array in php

青灯夜游
青灯夜游Original
2022-10-17 19:02:031821browse

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

How to find the smallest elements of an array in php

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(&#39;content-type:text/html;charset=utf-8&#39;);   
$arr=array(34,3,-1,-6,42,12,1);
echo "原数组:";
var_dump($arr);
asort($arr);
echo "升序排序后:";
var_dump($arr);
?>

How to find the smallest elements of an array in php

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(&#39;content-type:text/html;charset=utf-8&#39;);   
$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);

?>

How to find the smallest elements of an array in php

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 = SORT_STRING: Treat array elements as strings;

  • 3 = SORT_LOCALE_STRING: Compare array elements as strings based on the current locale (can be changed via setlocale()).

  • 4 = SORT_NATURAL: Similar to natsort(), it sorts strings in "natural order" for each array element. It is new in PHP5.4.0.

  • 5 = SORT_FLAG_CASE: Can be combined with SORT_STRING or SORT_NATURAL (OR bitwise operation), case-insensitive sorting string.

2. array_slice() function

array_slice() function can extract a fragment from the array

array array_slice ( array $arr , int $start [, int $length = NULL [, bool $preserve_keys = false ]] )

Parameters Description:

    arr represents the array to be intercepted.
  • start indicates the starting position (subscript) of interception:
    • If start is a positive number, interception will be performed from front to back.
    • If start is a negative number, start from the position -start from the end of arr and intercept from back to front. For example -2 means start from the second to last element of the array.
  • length is an optional parameter, indicating the intercepted length:
    • If length is a positive number, it indicates the number of intercepted elements;
    • If length If it is a negative number, then the intercepted fragment will end at the length position from the end of the array;
    • If it is omitted, it will start from the start position and continue to the end of the array.
  • preserve_keys is an optional parameter that specifies whether to retain the original key name. The default is false, that is, not retained; if set to true, the original key name will be retained.
Note: array_slice() function will not change the original array

Example 1: Parameter $start

<?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);
?>

How to find the smallest elements of an array in php

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);
?>

How to find the smallest elements of an array in php

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);
?>

How to find the smallest elements of an array in php

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!

Statement:
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