SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot zbing's Avatar
    Join Date
    Jun 2002
    Location
    Lisbon, Portugal
    Posts
    159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    sorting a multidimensional array

    Hi everyone,

    i'm having some trouble understanding how to sort a multidimensional array like this:

    Code:
    $foo=array(
    0 =>array(
    "name"=>"a",
    "score"=>"8",
    "time"=>"06:05:33"
    ),
    1 =>array(
    "name"=>"b",
    "score"=>"2",
    "time"=>"10:08:33"
    ),
    2 =>array(
    "name"=>"c",
    "score"=>"7",
    "time"=>"00:05:33"
    )
    );
    what i wanted to do was to be able to sort this array by any of the values of the "inner" arrays, eg. sort by name/score/time.

    cheers and thnx in advance,
    zbing

  2. #2
    SitePoint Addict fesh's Avatar
    Join Date
    Jan 2005
    Location
    Jackson, New Jersey
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    now what do you mean by sort? like put them in a specific order, or do you mean search?
    Fesh
    imagine what it's going to be like

  3. #3
    SitePoint Zealot zbing's Avatar
    Join Date
    Jun 2002
    Location
    Lisbon, Portugal
    Posts
    159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thnx for yer reply Fesh.
    i mean reordering the $foo array by one of the "column" names contained in the arrays inside it.

    i hope this is understandable...
    zbing

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is this what you have in mind?

    PHP Code:

    $foo 
    = array( =>array( "name"=>"a""score"=>"8""time"=>"06:05:33"),
                
    =>array( "name"=>"b""score"=>"2""time"=>"10:08:33"),
                
    =>array( "name"=>"c""score"=>"7""time"=>"00:05:33"),
              );



    array_multisort$foo[0], SORT_ASC,
                     
    $foo[1], SORT_ASC,
                     
    $foo[2], SORT_ASC
                     
    );
    print_r($foo);


    /*
    Array
    (
        [0] => Array
            (
                [time] => 06:05:33
                [score] => 8
                [name] => a
            )

        [1] => Array
            (
                [time] => 10:08:33
                [score] => 2
                [name] => b
            )

        [2] => Array
            (
                [time] => 00:05:33
                [score] => 7
                [name] => c
            )

    )
    */ 
    To sort each sub-array in descending order, change each "SORT_ASC" to "SORT_DSC". Since this function is similar to the behaviour of an SQL ORDER BY clause, you can specify different ordering values for each column/array as well (but the subsequent columns would only be ordered differently from the first where there are overlapping row values).

    Edit: This is probably more straight-forward for basic sorting:

    PHP Code:
    foreach($foo as $key => $val) {
        
    krsort($foo[$key]); // or ksort, etc.


  5. #5
    SitePoint Zealot zbing's Avatar
    Join Date
    Jun 2002
    Location
    Lisbon, Portugal
    Posts
    159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thnx for yer help k0ng,

    no, what i actually want is to reorder the top elements in the array $foo['0'], $foo['1'], $foo['2'] by one of the elements in the array they contain, eg "score":

    PHP Code:
    /* 
    Array 

        [0] => Array 
            ( 
                [time] => 06:05:33 
                [score] => 8 
                [name] => a 
            ) 
     
        [1] => Array 
            ( 
                [time] => 00:05:33 
                [score] => 7 
                [name] => c 
            ) 
     
        [2] => Array 
            ( 
                [time] => 10:08:33 
                [score] => 2 
                [name] => b 
            ) 
     

    */ 
    as if the $foo array was a result set from a select query and each of its elements was a row and u used the ORDER BY clause.

    cheers,
    zbing

  6. #6
    SitePoint Addict fesh's Avatar
    Join Date
    Jan 2005
    Location
    Jackson, New Jersey
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    do a loop that checks the array

    and if $foo[1]['score'] > $foo[0]['score']

    $temp = $foo[0];
    $foo[0] = $foo[1];
    $foo[1] = $temp;
    Fesh
    imagine what it's going to be like


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
  •