按名称排序复杂数组

时间:2010-12-24 20:31:54

标签: php arrays sorting

所以我正在阅读PHP中的排序数组,这很有意义,但它们只提供了简单的示例,所以我不确定如何对下面的代码进行排序。我想通过项目的标题对“学校用品”和“厨房需求”中的数组进行排序。我怎么做这样的事情?

非常感谢! 丹

Array
(
    [School Supplies] => Array
        (
            [0] => Array
                (
                    [item_id] => 1
                    [title] => Backpack
                    [parent_id] => 0
                    [amazon_url] => 
                )

            [1] => Array
                (
                    [item_id] => 2
                    [title] => Calculator
                    [parent_id] => 0
                    [amazon_url] => 
                )

            [2] => Array
                (
                    [item_id] => 3
                    [title] => Erasers
                    [parent_id] => 0
                    [amazon_url] => 
                )
        )

    [Kitchen Needs] => Array
        (
            [0] => Array
                (
                    [item_id] => 13
                    [title] => Bottle Opener
                    [parent_id] => 0
                    [amazon_url] => 
                )

             [1] => Array
                (
                    [item_id] => 14
                    [title] => Disposable Plates
                    [parent_id] => 0
                    [amazon_url] => 
                )

            [2] => Array
                (
                    [item_id] => 15
                    [title] => Disposable Silverware
                    [parent_id] => 0
                    [amazon_url] => 
                )
        )
)

1 个答案:

答案 0 :(得分:4)

function cmp($x, $y) {
    return strcmp($x['title'], $y['title']);
}

usort($array['School Supplies'], 'cmp');
usort($array['Kitchen Needs'], 'cmp');