uasort

(PHP 4, PHP 5, PHP 7)

uasort 使用用户自定义的比较函数对数组中的值进行排序并保持索引关联

说明

uasort ( array &$array , callable $value_compare_func ) : bool

本函数对数组排序并保持索引和单元之间的关联。

主要用于对那些单元顺序很重要的结合数组进行排序。比较函数是用户自定义的。

Note:

If two members compare as equal, their relative order in the sorted array is undefined.

参数

array

输入的数组。

value_compare_func

用户自定义比较函数的例子请参考 usort()uksort()

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

Example #1 uasort() 的基本例子

<?php
// Comparison function
function cmp($a$b) {
    if (
$a == $b) {
        return 
0;
    }
    return (
$a $b) ? -1;
}

// Array to be sorted
$array = array('a' => 4'b' => 8'c' => -1'd' => -9'e' => 2'f' => 5'g' => 3'h' => -4);
print_r($array);

// Sort and print the resulting array
uasort($array'cmp');
print_r($array);
?>

以上例程会输出:

Array
(
    [a] => 4
    [b] => 8
    [c] => -1
    [d] => -9
    [e] => 2
    [f] => 5
    [g] => 3
    [h] => -4
)
Array
(
    [d] => -9
    [h] => -4
    [c] => -1
    [e] => 2
    [g] => 3
    [a] => 4
    [f] => 5
    [b] => 8
)

参见

相关文章
php 数组 对数组进行排序php 数组 函数 用用户提供的回调函数做索引检查来计算数组的差集php 数组 函数 用回调函数过滤数组中的单元php 数组 函数 交换数组中的键和值php 数组 函数 带索引检查计算数组的交集,用回调函数比较索引php 数组 函数 对多个数组或多维数组进行排序php 数组 函数 带索引检查计算数组的差集,用回调函数比较数据php 数组 函数 带索引检查计算数组的差集,用回调函数比较数据和索引php 数组 函数 用回调函数比较数据来计算数组的差集php 数组 函数 带索引检查计算数组的交集,用回调函数比较数据php 数组 函数 带索引检查计算数组的交集,用单独的回调函数比较数据和索引php 数组 函数 对数组中的每个成员递归地应用用户函数php 数组 函数 使用用户自定义函数对数组中的每个元素做回调处理php 数组 函数 对数组进行逆向排序并保持索引关系php 数组 函数 对数组进行排序并保持索引关系php php 选项/信息 函数 返回所有常量的关联数组,键是常量名,值是常量值php 数组 函数 用“自然排序”算法对数组进行不区分大小写字母的排序php 数组 函数 使用用户自定义的比较函数对数组中的值进行排序并保持索引关联php 数组 函数 使用用户自定义的比较函数对数组中的键名进行排序php 数组 函数 使用用户自定义的比较函数对数组中的值进行排序
关注编程学问公众号