PHP数组排序函数合集 以及它们之间的联系分析
本篇文章是对PHP数组排序函数合集以及它们之间的联系进行了详细的分析介绍,需要的朋友参考下
下边提到的几个数组函数的排序有一些共性:
1 数组被作为排序函数的参数,排序以后,数组本身就发生了改变,函数的返回值为bool类型。
2 函数名中出现单a表示association,含义为,在按值排序的过程中,保持key=>value的对应关系不变
3 函数名中出现单k表示key,虚拟主机,含义为,在按值排序的过程中按照数组key而不是数组的值排序
4 函数名中出现单r的表示reverse,含义为,按照跟不加r的相反的顺序排列
5 函数名中出现单u的表示user-defined,含义为,使用用户自定义函数排序,如果函数的逻辑是参数1--------------------sort函数升序排序--------------------------------
复制代码 代码如下:
bool sort ( array &$array [, int $sort_flags= SORT_REGULAR ] )
$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
var_dump($fruits);
?>
结果:
array
0 =>
string
'apple' (length=5)
1 =>
string
'banana' (length=6)
2 =>
string
'lemon' (length=5)
3 =>
string
'orange' (length=6)
--------------------rsort降序排列--------------------
复制代码 代码如下:
$fruits = array("lemon", "orange", "banana", "apple");
rsort($fruits);
var_dump($fruits);
?>
结果:
array
0 =>
string
'orange' (length=6)
1 =>
string
'lemon' (length=5)
2 =>
string
'banana' (length=6)
3 =>
string
'apple' (length=5)
---------------asort按照二维数组值的升序排列(保持key=>value的关联关系)-----------
复制代码 代码如下:
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
asort($fruits);
var_dump($fruits);
?>
结果:
array
'c' =>
string
'apple' (length=5)
'b' =>
string
'banana' (length=6)
'd' =>
string
'lemon' (length=5)
'a' =>
string
'orange' (length=6)
---------arsort按照二维数组值的降序排列(保持key=>value的关联关系)---------
复制代码 代码如下:
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
arsort($fruits);
var_dump($fruits);
?>
结果
array
'a' =>
string
'orange' (length=6)
'd' =>
string
'lemon' (length=5)
'b' =>
string
'banana' (length=6)
'c' =>
string
'apple' (length=5)
--------------------ksort按照数组的key升序排列--------------
复制代码 代码如下:
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
ksort($fruits);
var_dump($fruits);
?>
结果
array
'a' =>
string
'orange' (length=6)
'b' =>
string
'banana' (length=6)
'c' =>
string
'apple' (length=5)
'd' =>
string
'lemon' (length=5)
---------------------krsort按照数组key的降序排列-----------------------
复制代码 代码如下:
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
krsort($fruits);
var_dump($fruits);
?>
array
'd' =>
string
'lemon' (length=5)
'c' =>
string
'apple' (length=5)
'b' =>
string
'banana' (length=6)
'a' =>
string
'orange' (length=6)
----------------usort函数按照用户自定义的函数排序----------------
复制代码 代码如下:
function cmp($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a }
$a = array(3, 2, 5, 6, 1);
usort($a, "cmp");
var_dump($a);
?>
结果:
array
0 =>
int
1
1 =>
int
2
2 =>
int
3
3 =>
int
5
4 =>
int
6
-----------------uksort使用自定义函数按照数组的key排序-----------------
复制代码 代码如下:
function cmp($a, $b)
{
$a = preg_replace('@^(a|an|the) @', '', $a);
$b = preg_replace('@^(a|an|the) @', '', $b);
return strcasecmp($a, $b);
}
$a = array("John" => 1, "the Earth" => 2, "an apple" => 3, "a banana" => 4);
uksort($a, "cmp");
var_dump($a);
?>
结果:
array
'an apple' =>
int
3
'a banana' =>
int
4
'the Earth' =>
int
2
'John' =>
int
1
--------------uasort将数组用自定义函数按照value排序,网站空间,保持索引关系不变---------
复制代码 代码如下:
// Comparison function
function cmp($a, $b) {
if ($a == $b) {
return 0;
}
return ($a }
// Array to be sorted
$array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);
var_dump($array);
// Sort and print the resulting array
uasort($array, 'cmp');
var_dump($array);
?>
结果:
array
'a' =>
int
4
'b' =>
int
8
'c' =>
int
-1
'd' =>
int
-9
'e' =>
int
2
'f' =>
int
5
'g' =>
int
3
'h' =>
int
-4
array
'd' =>
int
-9
'h' =>
int
-4
'c' =>
int
-1
'e' =>
int
2
'g' =>
int
3
'a' =>
int
4
'f' =>
int
5
'b' =>
int
8
-------------------array_multisort排序多个数组或多维数组---------
复制代码 代码如下:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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

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

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

In this chapter, we are going to learn the following topics related to routing ?

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

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

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