PHP ARRAY排在第三级

时间:2012-09-03 15:20:57

标签: php

您好我实际上是尝试使用不同的键对第三级数组进行排序。

以下是样本数组输入。

Array
(
    [19067] => Array
        (
            [Morrisville, PA] => 5.02
        )

    [18977] => Array
        (
            [Washington Crossing, PA] => 6.63
        )

    [08695] => Array
        (
            [Trenton, NJ] => 2.84
        )

    [08690] => Array
        (
            [Trenton, NJ] => 1
        )

    [08666] => Array
        (
            [Trenton, NJ] => 2.84
        )

    [08650] => Array
        (
            [Trenton, NJ] => 0
        )

    [08648] => Array
        (
            [Lawrence Township, NJ] => 2.88
        )

    [08647] => Array
        (
            [Trenton, NJ] => 2.84
        )

    [08646] => Array
        (
            [Trenton, NJ] => 2
        )

)

任何正文都可以帮我在第三级对上面的数组进行排序

预期结果应该是:

输出:

Array
(
    [08650] => Array
        (
            [Trenton, NJ] => 0
        )

    [08690] => Array
        (
            [Trenton, NJ] => 1
        )   

     [08646] => Array
        (
            [Trenton, NJ] => 2
        )

    [08666] => Array
        (
            [Trenton, NJ] => 2.84
        )   

    [08695] => Array
        (
            [Trenton, NJ] => 2.84
        )

    [08647] => Array
        (
            [Trenton, NJ] => 2.84
        )

    [08648] => Array
        (
            [Lawrence Township, NJ] => 2.88
        )

    [19067] => Array
        (
            [Morrisville, PA] => 5.02
        )

    [18977] => Array
        (
            [Washington Crossing, PA] => 6.63
        )   


)

提前致谢..

1 个答案:

答案 0 :(得分:2)

uasort($array, function (array $a, array $b) {
    return current($a) - current($b);
});

请注意注释:差异小于1.0的值将按相同方式排序。您可能希望执行(current($a) - current($b)) * 100或以其他方式舍入值,具体取决于您期望的值范围。