SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)

    Sort multidimentional array by an array value

    Hi folks
    Proper brain fart moment here....

    Say I have a multidimentional array such as:
    PHP Code:
    $d = array(
        
    "set1"=>array(
            
    "name"=>"Product 1",
            
    "price"=>"22.00"),
        
    "set2"=>array(
            
    "name"=>"Product 2",
            
    "price"=>"12.00"),
        
    "set3"=>array(
            
    "name"=>"Product 3",
            
    "price"=>"244.00")
        ); 
    I need to sort it by price....

    help!
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  2. #2
    SitePoint Addict
    Join Date
    Aug 2005
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    uasort();


    PHP Code:
    <?php

    $d 
    = array(

        
    "set1"=>array(

            
    "name"=>"Product 1",

            
    "price"=>"22.00"),

        
    "set2"=>array(

            
    "name"=>"Product 2",

            
    "price"=>"12.00"),

        
    "set3"=>array(

            
    "name"=>"Product 3",

            
    "price"=>"244.00")

        );

    // sort low to high

    uasort $dcreate_function '$a,$b''return ( $a["price"] == $b["price"] ? 0 : ( $a["price"] < $b["price"] ? -1 : 1 ) );' ) );

    print_r $d );

    // sort high to low

    uasort $dcreate_function '$a,$b''return ( $a["price"] == $b["price"] ? 0 : ( $a["price"] > $b["price"] ? -1 : 1 ) );' ) );

    print_r $d );

    ?>

  3. #3
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    nice
    Thanks
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Here's another way, not saying its better, its just that I am swotting up on this in any case ...

    PHP Code:
    $d = array(
        
    "set1"=>array(
            
    "name"=>"Product 1",
            
    "price"=>"22.00"),
        
    "set2"=>array(
            
    "name"=>"Product 2",
            
    "price"=>"12.00"),
        
    "set3"=>array(
            
    "name"=>"Product 3",
            
    "price"=>"244.00")
        ); 

    //reassign the values if "set1" is meaningless

    foreach ( $d as $k=>$v ){
    // offers the possibilty to make sure price is a FLOAT too
    $product$v['price'] ] = $v['name'];  
    }

    ksort($product) ;

    // print_r($product) ;
    // gives
    // Array ( [12.00] => Product 2 [22.00] => Product 1 [244.00] => Product 3 ) 

  5. #5
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    also nice cups buuuuuuut forgot to mention that there are 6 other value in the array besides name and price
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    typical, do you expect me to read between the lines? (tehe...)

  7. #7
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by printf View Post
    PHP Code:

    uasort 
    $dcreate_function '$a,$b''return ( $a["price"] == $b["price"] ? 0 : ( $a["price"] < $b["price"] ? -1 : 1 ) );' ) ); 
    This can be expressed much simpler using subtraction:

    PHP Code:
    uasort($dcreate_function('$a,$b'
        
    'return $a["price"] - $b["price"];')); 


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
  •