Get the App
SLTechnology News&Howtos  ›  Development  › 

How to remove the largest and smallest elements from the php array

Shulou Source: shulou.com Published: 2022-05-31 17:51:58 09月11日 Update

This article mainly introduces the php array how to remove the maximum and minimum elements of the relevant knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that after reading this php array how to remove the maximum and minimum elements of the article will have a harvest, let's take a look.

Remove the method: 1, use "sort ($arr)" to sort the array in ascending order, after sorting, the first element of the array is the minimum, and the last element is the maximum; 2, delete the last element with "array_pop ($arr)" and delete the first element with "array_shift ($arr)".

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

The method of removing the largest and smallest elements from php Array

Step 1. Use sort () to sort the array in ascending order

As you can see, the first element of the array after sorting is the minimum value, and the last element is the maximum value. You only need to delete these two values.

Step 2. Delete the maximum and minimum values (the first and last elements of the array)

So how to delete the first and last element of the array, you can use the array_pop () and array_shift () functions

Array_pop (): deletes the last element in the array (off the stack).

Array_shift (): deletes the first element in the array.

Array_pop ($arr); / / remove the maximum var_dump ($arr)

Array_shift ($arr); / / remove the minimum var_dump ($arr)

Note: the array_pop () and array_shift () functions modify the original array.

This is the end of the article on "how to remove the largest and smallest elements from the php array". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to remove the maximum and minimum elements of php array". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Element array maximum minimum that is maximum sort knowledge content function ascending order method step article two value just easy to operate tutorial article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi NVidia Shulou Tech Info Redmi Microsoft