SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2005
    Posts
    103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    sorting a multidimensional array

    Hello,

    I'm looking for a way to sort a multidimensional array.

    My array is:

    Code:
    $recipes
    
    Array
    (
        [0] => Array
            (
                [recipe_id] => 57
                [fromuser] => 1
                [recipe_name] => Arroz Con Pollo
                [preparation] => n/a
                [cook_time] => n/a
                [servings] => 4
                [rate] => 0
                [has_picture] => 1
                [created_date] => 0000-00-00 00:00:00
                [have] => 1
                [percentage] => 100
            )
    
        [1] => Array
            (
                [recipe_id] => 42972
                [fromuser] => 1
                [recipe_name] => Beef and Bean Chimichangas
                [preparation] => 15 Minutes
                [cook_time] => 30 Minutes
                [servings] => 8
                [rate] => 0
                [has_picture] => 0
                [created_date] => 0000-00-00 00:00:00
                [have] => 2
                [percentage] => 2
            )
    
        [2] => Array
            (
                [recipe_id] => 191
                [fromuser] => 1
                [recipe_name] => Beef Enchiladas
                [preparation] => 20 minutes
                [cook_time] => 35 minutes
                [servings] => 4
                [rate] => 0
                [has_picture] => 1
                [created_date] => 0000-00-00 00:00:00
                [have] => 4
                [percentage] => 34
            )
    )
    I'm trying to sort it by percentage.

    Thanks for any help or leads.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function cmp_percentage($a$b) {
        return 
    $b['percentage'] - $a['percentage'];
    }
    usort($arr'cmp_percentage'); 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •