PHP排序多维数组usort()

时间:2012-02-14 11:27:25

标签: php arrays usort

我有以下数组,并希望通过“名称”按字母顺序排序。我有点困惑如何使用usort()函数,因为我没有工作,或者有更好的功能使用?

Array (
    [0] => SimpleXMLElement Object
        (
            [id] => 1118809
            [Name] => Laptop
            [parentID] => 0
            [sequence] => 4
            [visible] => 1
        )

    [1] => SimpleXMLElement Object
        (
            [id] => 1109785
            [Name] => Special Offers
            [parentID] => 0
            [sequence] => 0
            [visible] => 1
        )

    [2] => SimpleXMLElement Object
        (
            [id] => 1118805
            [Name] => Printers
            [parentID] => 0
            [sequence] => 12
            [visible] => 0
        )

    [3] => SimpleXMLElement Object
        (
            [id] => 1092140
            [Name] => USB
            [parentID] => 0
            [sequence] => 14
            [visible] => 1
        ) )

function sort_cats_by_name($a, $b) {
    return   $a->Name  - $b->Name;
}

usort($subcats, 'sort_cats_by_name');

1 个答案:

答案 0 :(得分:1)

这个应该更好。

function sort_cats_by_name($a, $b) {
   return   strcmp($a->Name,$b->Name);
}