按键值的PHP订单数组

时间:2012-07-18 16:51:10

标签: php arrays sorting

我知道之前已发布过,但我无法让它为我工作:我不知道为什么!但是任何我的数组$服务器都是这样的

Array
(
    [0] => Array
        (
            [0] => Dell PowerEdge R210 II
            [1] => 8
            [2] => Array
                (
                    [0] => 8
                    [1] => Sock
                    [2] => Price
                    [3] => ImageURL
                    [4] => CPU
                    [5] => Memory
                    [6] => HDD
                    [7] => Bandwidth
                    [8] => OS
                    [9] => Upgrades
                    [10] => OrderLink
                )

        )

    [1] => Array
        (
            [0] => Dell PowerEdge R210 II 2
            [1] => 2
            [2] => Array
                (
                    [0] => 2
                    [1] => Sock
                    [2] => Price
                    [3] => ImageURL
                    [4] => CPU
                    [5] => Memory
                    [6] => HDD
                    [7] => Bandwidth
                    [8] => OS
                    [9] => Upgrades
                    [10] => OrderLink
                )

        )

    [2] => Array
        (
            [0] => Dell PowerEdge R210 II 3
            [1] => 3
            [2] => Array
                (
                    [0] => 3
                    [1] => Sock
                    [2] => Price
                    [3] => ImageURL
                    [4] => CPU
                    [5] => Memory
                    [6] => HDD
                    [7] => Bandwidth
                    [8] => OS
                    [9] => Upgrades
                    [10] => OrderLink
                )

        )

    [3] => Array
        (
            [0] => Dell PowerEdge R210 II 4
            [1] => 4
            [2] => Array
                (
                    [0] => 4
                    [1] => Sock
                    [2] => Price
                    [3] => ImageURL
                    [4] => CPU
                    [5] => Memory
                    [6] => HDD
                    [7] => Bandwidth
                    [8] => OS
                    [9] => Upgrades
                    [10] => OrderLink
                )

        )

    [4] => Array
        (
            [0] => Dell PowerEdge R210 II 5
            [1] => 5
            [2] => Array
                (
                    [0] => 5
                    [1] => Sock
                    [2] => Price
                    [3] => ImageURL
                    [4] => CPU
                    [5] => Memory
                    [6] => HDD
                    [7] => Bandwidth
                    [8] => OS
                    [9] => Upgrades
                    [10] => OrderLink
                )

        )

    [5] => Array
        (
            [0] => Dell PowerEdge R210 II 6
            [1] => 6
            [2] => Array
                (
                    [0] => 6
                    [1] => Sock
                    [2] => Price
                    [3] => ImageURL
                    [4] => CPU
                    [5] => Memory
                    [6] => HDD
                    [7] => Bandwidth
                    [8] => OS
                    [9] => Upgrades
                    [10] => OrderLink
                )

        )

)

我想重新安排阵列并将其重新安装到$服务器中,但按$服务器顺序排列[x] [1]

所以,如果奇数是(第一个对象是8),我希望用最小的数字1对它进行排序,移动到最大的数字!

非常感谢

ps我试过这个

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

$a = array(3, 2, 5, 6, 1);

usort($servers, "cmp");
?>

但它似乎没有用!

由于

1 个答案:

答案 0 :(得分:1)

您的cmp错了。 $a$b

之类的数组
Array
    (
        [0] => Dell PowerEdge R210 II 6
        [1] => 6
        [2] => Array
            (
                [0] => 6
                [1] => Sock
                [2] => Price
                [3] => ImageURL
                [4] => CPU
                [5] => Memory
                [6] => HDD
                [7] => Bandwidth
                [8] => OS
                [9] => Upgrades
                [10] => OrderLink
            )

    )

您必须将$a[1]$b[1]进行比较:

function cmp($a, $b) {
    return $a[1] - $b[1];
}