Home php教程 php手册 解析PHP中数组元素升序、降序以及重新排序的函数

解析PHP中数组元素升序、降序以及重新排序的函数

Jun 13, 2016 am 11:46 AM
php range element function create Ascending order fast sort array of parse descending order

1,快速创建数组的函数range()
比如range()函数可以快速创建从1到9的数字数组:

复制代码 代码如下:


$numbers=range(1,9);
echo $numbers[1];
?>


当然,使用range(9,1)则创建了9到1的数字数组。同时,range()还可以创建从a到z 的字符数组:

复制代码 代码如下:


$numbers=range(a,z);
foreach ($numbers as $mychrs)
    echo $mychrs." ";
?>


使用字符数组时注意大小写,比如range(A,z)和range(a,Z)是不一样的。
range()函数还具有第三个参数,该参数的作用是设定步长,比如range(1,9,3)创建的数组元素是:1、4、7

2,PHP中常规数组的排序
一般数组中的各元素均以字符或数字表现的,所以可对数组元素进行升序排列,该功能函数为sort()。比如:

复制代码 代码如下:


$people=array('name','sex','nation','birth');
foreach ($people as $mychrs)
    echo $mychrs." ";
sort($people);
echo "
---排序后---
";
foreach ($people as $mychrs)
    echo $mychrs." ";
?>


升序排序后的数组元素显示为 birth name nation sex,当然,sort()函数是区分字母大小写的(字母从大到小的顺序是:A…Z…a…z)
Sort()函数还具有第二参数,用来说明升序的规则是用来比较数字还是字符串的。比如:

复制代码 代码如下:


echo "---按数字升序排序---
";
$num2=array('26','3',);
sort($num2,SORT_NUMERIC);
foreach ($num2 as $mychrs)
    echo $mychrs." ";
echo "
---按字符升序排序---
";
$num3=array('26','3');
sort($num3,SORT_STRING);
foreach ($num3 as $mychrs)
    echo $mychrs." ";
?>


SORT_NUMERIC和SORT_STRING用来声明按数字或字符的升序排列。如果按照数字升序排列是:3,26;但如果按照字符升序排列则是:26,3了。
PHP中除了升序函数以外,还有降序或称反向排列的函数,就是rsort()函数,比如:

复制代码 代码如下:


$num1=range(1,9);
rsort($num1);


这里其实就相当于range(9,1)

3,PHP中关联数组的排序
PHP除了支持数字索引数组以外,还支持相关数组。比如如下数组就是一个相关(关联)数组:

复制代码 代码如下:


$peoples=array('xm'=>'name','xb'=>'sex','mz'=>'nation','cs'=>'birth');


使用sort($peoples)默认即是按照元素定义值的升序排序,在关联数组中可使用asort()函数表示,关联数组中最主要的则是可按照关键字(如xm、xb、mz等)的升序排序,该方法是用函数ksort()函数。

复制代码 代码如下:


$peoples=array('xm'=>'name','xb'=>'sex','mz'=>'nation','cs'=>'birth');
foreach ($peoples as $mychrs)
    echo $mychrs." ";
echo "
--按元素值升序排列--
";
asort($peoples);
foreach ($peoples as $mychrs)
    echo $mychrs." ";
echo "
--按关键字升序排列--
";
ksort($peoples);
foreach ($peoples as $mychrs)
    echo $mychrs." ";
?>


和常规数组拥有sort()升序函数的反向排序rsort()降序函数相对应的,关联数组也有对应的降序函数:asort()函数和arsort()函数、ksort()函数和krsort()函数。
记忆:原型函数是sort(),其中a、k表示关联数组相关必须前置,反向排序使用r修饰。

4,PHP数组元素随机排序
PHP中使用shuffle()函数将数组元素进行随机的重新排序,比如:

复制代码 代码如下:


$fer=array('cnbruce','cnrose','cnjames','cnanne');
shuffle($fer);
foreach ($fer as $mychrs)
echo $mychrs." ";
?>


每次都会显示不同的排序组合

5,PHP数组按原顺序反向排序
PHP中可使用array_reverse()函数将数组元素按原顺序反向排序。比如:

复制代码 代码如下:


$fer=array('cnbruce','cnrose','cnjames','cnanne');
foreach ($fer as $mychrs)
echo $mychrs." ";
$fer=array_reverse($fer);
echo "
--按原顺序反向--
";
foreach ($fer as $mychrs)
echo $mychrs." ";
?>


cnbruce cnrose cnjames cnanne
--按原顺序反向--
cnanne cnjames cnrose cnbruce
注意这里的 $fer=array_reverse($fer); 在此之前的任何排序函数均只是声明引用,并没有对原有数组进行重新定义,但当我调试该函数时则需要重新定义。这是因为array_reverse()是返回原数组修改后的副本,如果不需要原有数组,可对原数组重新定义达到覆盖的目的,否则就另外定义数组保存副本,比如:$fer_bak=array_reverse($fer);
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

See all articles