Home  >  Article  >  Backend Development  >  PHP数组排序之sort、asort与ksort用法实例_php技巧

PHP数组排序之sort、asort与ksort用法实例_php技巧

WBOY
WBOYOriginal
2016-05-16 20:36:302139browse

本文实例讲解了PHP数组排序中sort、asort与ksort的用法,供大家参考借鉴之用。具体实例如下所示:

<&#63;php
$arr = array('d'=>'sdf', 'r'=>'sdf', 'a'=> 'eee');
//sort($arr);  // 对数组的值进行重排, 删除之前的键值, 变为索引数组
//asort($arr); // 对数组按照值进行重排,并保持索引关系,索引数组和关联数组均适用
ksort($arr); // 对数组按照键值进行重排,并保持索引关系,索引数组和关联数组均适用

// 对应逆序还有rsort arsort krsort
// 使用函数比较有usort uksort uasort  第二个参数为比较的函数 需要在第一个参数相等 小于 大于第二个参数时 返回 等于 小于 大于 0 的值 浮点数只会取整数部分

print_r($arr);
&#63;>

本文实例中仅对ksort用法做了示范,感兴趣的读者可以再测试sort与asort的运行结果,以便加深印象,牢固掌握。希望本文实例对大家PHP程序设计有所帮助。

Statement:
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