Home php教程 php手册 PHP数组排序函数使用方法

PHP数组排序函数使用方法

Jun 13, 2016 am 09:50 AM
php and Instructions function exist sort data array have Simple

在php中数据排序函数有很多,包括有一维数组排序函数与二维数组排序函数,包括简单sort函数升序排序,rsort降序排列等

php数组排序函数有

sort  (  &$arr   [,fruits] )  对数组进行从低到高排序 ,并赋予新的键名 返回bool
rsort  ( &$arr   [,fruits] ) 对数组进行逆向排序  并赋予新的键名
asort ( &$arr   [,fruits] ) 对数组进行排序 ,并保持索引不变
arsort( &$arr   [,fruits] ) 对数组进行逆向排序 并保持索引不变


ksort ( &$arr [,fruits] ) 对数组按照键名进行排序
krsort( &$arr [,fruits] ) 对组数按照键名进行逆向排序


natsort( &$arr )     对数组键值进行‘自然排序法’ 按照长度 字母排序等
natcasesort( &$arr ) 对数组进行不区分大小写的 ‘自然排名’


usort ( &$arr , cmp_function ) 用户自定义函数对一个数组进行排序 重新排列键名
uksort (&$arr , cmp_function ) 用户自定义函数对一个数组进行键名排序
uasort (&$arr , cmp_function) 用户自定义函数对数组进行排序 并保持索引不变


array_multisort( $arr , mixed)

第二个参数是 可以根据值改变排序行为


SORT_REGULAR 正常比较单元  SORT_NUMERIC 单元被作为数字来比较 
SORT_STRING 单元被作为字符串来比较 SROT_LOCALE_STRING 根据当前的local 设置来把单元当做字符串比较


--------------------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排序多个数组或多维数组---------

 代码如下 复制代码

$ar = array(
       array("10", 11, 100, 100, "a"),
       array(   1,  2, "2",   3,   1)
      );
  
array_multisort($ar[0], SORT_ASC, SORT_STRING,
                $ar[1], SORT_NUMERIC, SORT_DESC);
var_dump($ar);
?>
结果:
array
  0 =>
    array
      0 =>

string

 '10' (length=2)
      1 =>

int

 100
      2 =>

int

 100
      3 =>

int

 11
      4 =>

string

 'a' (length=1)
  1 =>
    array
      0 =>

int

 1
      1 =>

int

 3
      2 =>

string

 '2' (length=1)
      3 =>

int

 2
      4 =>

int

1

//说明:
1 上例中:$ar数组优先按照$ar[0]的字符串值升序排列,如果字符串值相等,再按照$ar[1]数组的数字值降序排列。
2 array_multisort函数的任意一个位置的参数如果是数组,表示排序时用的值,
如果有多个数组参数,优先按照前边的数组值进行排序,如果是常量,例如
SORT_ASC, SORT_DESC, SORT_REGULAR,SORT_NUMERIC, SORT_STRING.


PHP二维数组排序函数

PHP一维数组的排序可以用sort(),asort(),arsort()等函数,但是PHP二维数组的排序需要自定义。

以下函数是对一个给定的二维数组按照指定的键值进行排序,先看函数定义:

 代码如下 复制代码

function array_sort($arr,$keys,$type='asc'){
 $keysvalue = $new_array = array();
 foreach ($arr as $k=>$v){
  $keysvalue[$k] = $v[$keys];
 }
 if($type == 'asc'){
  asort($keysvalue);
 }else{
  arsort($keysvalue);
 }
 reset($keysvalue);
 foreach ($keysvalue as $k=>$v){
  $new_array[$k] = $arr[$k];
 }
 return $new_array;
}

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
4 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