php 数组排序在php中有着重要的作用,本篇对其作出相关的详解。
php - 数组排序函数
在本章中,我们将一一介绍下列 php 数组排序函数:
sort() - 对数组进行升序排列
rsort() - 对数组进行降序排列
asort() - 根据关联数组的值,对数组进行升序排列
ksort() - 根据关联数组的键,对数组进行升序排列
arsort() - 根据关联数组的值,对数组进行降序排列
krsort() - 根据关联数组的键,对数组进行降序排列
sort() - 对数组进行升序排列
下面的实例将 $cars 数组中的元素按照字母升序排列:
实例
<?php
$cars=array("volvo","bmw","toyota");
sort($cars);
?>
下面的实例将 $numbers 数组中的元素按照数字升序排列:
实例
<?php
$numbers=array(4,6,2,22,11);
sort($numbers);
?>
rsort() - 对数组进行降序排列
下面的实例将 $cars 数组中的元素按照字母降序排列:
实例
<?php
$cars=array("volvo","bmw","toyota");
rsort($cars);
?>
下面的实例将 $numbers 数组中的元素按照数字降序排列:
实例
<?php
$numbers=array(4,6,2,22,11);
rsort($numbers);
?>
asort() - 根据数组的值,对数组进行升序排列
下面的实例根据数组的值,对关联数组进行升序排列:
实例
<?php
$age=array("peter"=>"35","ben"=>"37","joe"=>"43");
asort($age);
?>
ksort() - 根据数组的键,对数组进行升序排列
下面的实例根据数组的键,对关联数组进行升序排列:
实例
<?php
$age=array("peter"=>"35","ben"=>"37","joe"=>"43");
ksort($age);
?>
arsort() - 根据数组的值,对数组进行降序排列
下面的实例根据数组的值,对关联数组进行降序排列:
实例
<?php
$age=array("peter"=>"35","ben"=>"37","joe"=>"43");
arsort($age);
?>
krsort() - 根据数组的键,对数组进行降序排列
下面的实例根据数组的键,对关联数组进行降序排列:
实例
<?php
$age=array("peter"=>"35","ben"=>"37","joe"=>"43");
krsort($age);
?>
本篇对php数组排序作出了相关的详解,更多的学习资料清关注即可观看。
相关推荐:
php 文件上传的相关知识与运用
php 循环 - while 循环的了解与使用
php 5 echo 和 print 语句的相关知识与运用
以上就是关于php 数组排序相关的知识运用的详细内容。