排序多级数组php

时间:2014-02-25 11:12:50

标签: php arrays sorting multidimensional-array

我有以下数组:

array(
    array(
            id: 4,
            name: car,
            pid: 0
        ),
        array(
            id: 5,
            name: lights,
            pid: 4
        ),
        array(
            id: 6,
            name: fog,
            pid: 5
        )
    ),
    array(
        array(
            id: 1,
            name: bike,
            pid: 0
        ),
        array(
            id: 2,
            name: wheel,
            pid: 1
        ),
        array(
            id: 3,
            name: tire,
            pid: 2
        )
    ),
    array(
            id: 7,
            name: car,
            pid: 0
        ),
        array(
            id: 8,
            name: lights,
            pid: 7
        ),
        array(
            id: 9,
            name: brake,
            pid: 8
        )
    ),
    array(
            id: 10,
            name: car,
            pid: 0
        ),
        array(
            id: 11,
            name: engine,
            pid: 10
        )
    ),
)

这些multidimesnional数组代表汽车的规格。在表格中,它看起来像:

| car -> lights -> fog |
| bike -> wheel -> tire |
| car -> lights -> brake |
| car -> engine |

现在我想对名称中每个级别的数组进行排序。首先,排序第一列,而不是排序第二列等......

我们的排序数组应如下所示:

| bike -> wheel -> tire |
| car -> engine |
| car -> lights -> brake |
| car -> lights -> fog |

使用usort我可以对第一列进行排序:

usort($characteristics, function($a, $b){
    return $a[0]['name'] > $b[0]['name'];
});

但是当然这仅影响第一列,在这种情况下,数组看起来像:

| bike -> wheel -> tire |
| car -> lights -> fog |
| car -> lights -> brake |
| car -> engine |

也可以有不同的长度(比如发动机,有2个值,而自行车有3个)。通常长度在1到5之间变化。

我宁愿不会为/ foreach循环使用太多,因为这会使我的程序太慢。对于这个冗长的问题也很抱歉,但我找不到更简短的方法来解释我的问题。

我希望有人可以给我一个简单的清洁解决方案。

1 个答案:

答案 0 :(得分:0)

我相信这就是你要找的东西:

$arr =
    array(
        array(
            array(
                "id"   => 4,
                "name" => "car",
                "pid"  => 0
            ),
            array(
                "id"   => 5,
                "name" => "lights",
                "pid"  => 4
            ),
            array(
                "id"   => 6,
                "name" => "fog",
                "pid"  => 5
            )
        ),
        array(
            array(
                "id"   => 1,
                "name" => "bike",
                "pid"  => 0
            ),
            array(
                "id"   => 2,
                "name" => "wheel",
                "pid"  => 1
            ),
            array(
                "id"   => 3,
                "name" => "tire",
                "pid"  => 2
            )
        ),
        array(
            array(
                "id"   => 7,
                "name" => "car",
                "pid"  => 0
            ),
            array(
                "id"   => 8,
                "name" => "lights",
                "pid"  => 7
            ),
            array(
                "id"   => 9,
                "name" => "brake",
                "pid"  => 8
            )
        ),
        array(
            array(
                "id"   => 10,
                "name" => "car",
                "pid"  => 0
            ),
            array(
                "id"   => 11,
                "name" => "engine",
                "pid"  => 10
            )
        )
    );

for ($i = 0; $i < count($arr); $i++) {
    foreach ($arr[$i] as $a) {
        foreach($a as $key => $value) {
            if ($key == 'name') {
                $newArr[$i][] = $value;
            }
        }
    }
}

usort($newArr, function($a, $b) {
    return $a[0] > $b[0];
});

echo "<pre>", print_r($newArr), "</pre>";