1

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

1 Answers1

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

usort($array['School Supplies'], 'cmp');
usort($array['Kitchen Needs'], 'cmp');
Jonah
  • 9,991
  • 5
  • 45
  • 79