开发者

Sorting Complex Arrays by Name

开发者 https://www.devze.com 2023-02-01 17:11 出处:网络
So I was reading up on sorting arrays in PHP and it makes sense, but they only gave easy examples, so I\'m not sure how I would sort something the code below. I want to sort the arrays inside the \"Sc

So I was reading up on sorting arrays in PHP and it makes sense, but they only gave easy examples, so I'm not sure how I would sort something the code below. I want to sort the arrays inside the "School Supplies" and "Kitchen Needs" by the title of the item. How would I do such a thing?

Thanks a lot! Dan

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] => 
                )
        )
)


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

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

精彩评论

暂无评论...
验证码 换一张
取 消