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?
Very strage. Try to make $this_array = array("3"=>'apple', "2"=>'banana', "6"=>'grape', "4"=>'orange'); for the sake of checking.
$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
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.
... And you have 3 levels of arrays here, which level do you want to sort the second one or the third one?
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. . 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 . Thomas
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.