Sorting an array of arrays

Discussion in 'PHP' started by Weirfire, Jul 25, 2006.

  1. #1
    I'm trying to sort an array of arrays as is detailed below


    $this_array = array(3=>'apple', 2=>'banana', 6=>'grape', 4=>'orange');

    ksort($this_array);

    The ksort is supposed to sort by the key to give

    2 =>
    3 =>
    4 =>
    6 =>

    but I'm getting the same order. Anyone got any ideas?
     
    Weirfire, Jul 25, 2006 IP
  2. hextraordinary

    hextraordinary Well-Known Member

    Messages:
    2,171
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    105
    #2
    Very strage.

    Try to make $this_array = array("3"=>'apple', "2"=>'banana', "6"=>'grape', "4"=>'orange');

    for the sake of checking.
     
    hextraordinary, Jul 25, 2006 IP
  3. aras

    aras Active Member

    Messages:
    533
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #3
    $this_array = array(3=>'apple', 2=>'banana', 6=>'grape', 4=>'orange');

    ksort($this_array);

    foreach($this_array as $key => $value) {
    echo "$key : $value";
    }

    what does this output
     
    aras, Jul 25, 2006 IP
  4. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #4
    lol when I try the actual example I gave above it works but the complicated version doesnt.
     
    Weirfire, Jul 25, 2006 IP
  5. hextraordinary

    hextraordinary Well-Known Member

    Messages:
    2,171
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    105
    #5
    Whats the "complicated version" than.....LOL

    Can't help you with no code...
     
    hextraordinary, Jul 25, 2006 IP
  6. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #6
    I'm trying to sort the following array

    array(15) { [0]=> array(1) { ["ag"]=> array(1) { ["july102005.php"]=> string(13) "July 10, 2005" } } [1]=> array(1) { ["ac"]=> array(1) { ["march132005.php"]=> string(14) "March 13, 2005" } } [2]=> array(1) { ["ae"]=> array(1) { ["may052005.php"]=> string(12) "May 05, 2005" } } [3]=> array(1) { ["ac"]=> array(1) { ["march312005.php"]=> string(14) "March 31, 2005" } } [4]=> array(1) { ["ai"]=> array(1) { ["september172005.php"]=> string(18) "September 17, 2005" } } [5]=> array(1) { ["ad"]=> array(1) { ["april042005.php"]=> string(14) "April 04, 2005" } } [6]=> array(1) { ["aj"]=> array(1) { ["october242005.php"]=> string(16) "October 24, 2005" } } [7]=> array(1) { ["aa"]=> array(1) { ["january242005.php"]=> string(16) "January 24, 2005" } } [8]=> array(1) { ["af"]=> array(1) { ["june042005.php"]=> string(13) "June 04, 2005" } } [9]=> array(1) { ["ah"]=> array(1) { ["august122005.php"]=> string(15) "August 12, 2005" } } [10]=> array(1) { ["ab"]=> array(1) { ["february152005.php"]=> string(17) "February 15, 2005" } } [11]=> array(1) { ["ac"]=> array(1) { ["march022005.php"]=> string(14) "March 02, 2005" } } [12]=> array(1) { ["bg"]=> array(1) { ["july242006.php"]=> string(13) "July 24, 2006" } } [13]=> array(1) { ["aa"]=> array(1) { ["january302005.php"]=> string(16) "January 30, 2005" } } [14]=> array(1) { ["ab"]=> array(1) { ["february062005.php"]=> string(17) "February 06, 2005" } } }

    As you can see its an array of archives.


    I think part of the problem is that the key of each item isn't aa, ab etc but is 1, 2, 3 defined by when I created each array item. Since each date is unique I probably need to set each key when assign array values.
     
    Weirfire, Jul 25, 2006 IP
  7. hextraordinary

    hextraordinary Well-Known Member

    Messages:
    2,171
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    105
    #7
    That shouldn't be a problem. Whats the sort code that you use to sort this mess now?
     
    hextraordinary, Jul 25, 2006 IP
  8. hextraordinary

    hextraordinary Well-Known Member

    Messages:
    2,171
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    105
    #8
    ... And you have 3 levels of arrays here, which level do you want to sort the second one or the third one?
     
    hextraordinary, Jul 25, 2006 IP
  9. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #9
    I want to sort in reverse order of bg -> aa
     
    Weirfire, Jul 25, 2006 IP
  10. coderlinks

    coderlinks Peon

    Messages:
    282
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #10
    You can try this. Sort each level of array individually. Maybe some code like:

    
    $array_mess = (blah blah...); // <-- You know what this is :D
    $array_sorted = Array();
    foreach($array_mess as $key1=>$arr_lvl1)
    {
         ksort($arr_lvl1);
         foreach($arr_lvl1 as $key2=>$arr_lvl2)
         {
                 ksort($arr_lvl2);
                 foreach($arr_lvl2 as $key3=>$arr_lvl3)
                 {
                        ksort($arr_lvl3);
                        $arr_lvl2[$key3]=$arr_lvl3;
                 }
                 $arr_lvl1[$key2]=$arr_lvl2;
         }
         $array_sorted[$key1]=$arr_lvl1;
    }
    print_r($array_sorted); // <-- This should show the sorted array
    
    PHP:
    There is a VERY HIGH probability that this code will fail. :D . I haven't tested it. I just typed in on the fly. So I disclaim any responsibility of anything that happens if you use this script :D .

    Thomas :D
     
    coderlinks, Jul 25, 2006 IP
  11. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #11
    ok what I've done is instead of having 1 => 2 => as the keys I've attached aa ab as keys so now it will sort the keys with the krsort and display the dates in reverse order starting with the newest. Its an array inside an array but it does the job nicely now.

    Thank goodness that ordeal is over.
     
    Weirfire, Jul 25, 2006 IP
  12. coderlinks

    coderlinks Peon

    Messages:
    282
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Ahh... I made all that code for nothing... :( ;)

    Thomas
     
    coderlinks, Jul 25, 2006 IP
    Weirfire likes this.
  13. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #13
    You never know. Someone else that comes across this problem might be able to use the code. :)
     
    Weirfire, Jul 25, 2006 IP