Aeterna Posted July 19, 2011 Share Posted July 19, 2011 Hi all, I'm trying to sort my array, which is something along the lines of: $myArray = array('Test' => 120, 'Tust' => 394, 'Lap' => 439493); What I'm trying to do, is sort the keys (which I've done), and then add the names (Test, Tust, Lap) to an array in the order the keys ordered them, so that I can display them in the order they belong in (the order the keys put them in). I can't figure it out for the life of me... here's what I'm currently using: function associative_push($arr, $tmp) { if (is_array($tmp)) { foreach ($tmp as $key => $value) { $arr[$key] = $value; } return $arr; } return false; } $participants = array('Lap', 'Test', 'Tust'); $starting = array(); $current = array(); for ($a = 0; $a < count($participants); $a++) { $starting = associative_push($starting, array($participants[$a] => "testing: $a")); } for ($b = 0; $b < count($participants); $b++) { $current = associative_push($current, array($participants[$b] => "testing: $b")); } arsort($starting); arsort($current); for ($i = 0; $i < count($participants); $i++) { $startingExperience = $starting[$participants[$i]]; $currentExperience = $current[$participants[$i]]; // table stuff here (bunch of unnecessary code) } I'm trying to get it so that 'Lap' would be the first table entry, 'Tust' would be the second, and 'Test' the third. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/242324-associative-array-sorting-and-then-matching/ Share on other sites More sharing options...
WebStyles Posted July 19, 2011 Share Posted July 19, 2011 I'm trying to get it so that 'Lap' would be the first table entry, 'Tust' would be the second, and 'Test' the third. Any ideas? That makes the order non-alphabetical. Are you just trying to reverse the order of the original array? Quote Link to comment https://forums.phpfreaks.com/topic/242324-associative-array-sorting-and-then-matching/#findComment-1244552 Share on other sites More sharing options...
AyKay47 Posted July 19, 2011 Share Posted July 19, 2011 From what you said, you don't want the keys in alphabetical order here? My thoughts were to use ksort here and store the key into an array...not sure what exactly you are trying to do here. $myArray = array('Test' => 120, 'Tust' => 394, 'Lap' => 439493); ksort($myArray,SORT_REGULAR); foreach($myArray as $key => $value){ $arr[$key] = $value; } Quote Link to comment https://forums.phpfreaks.com/topic/242324-associative-array-sorting-and-then-matching/#findComment-1244562 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.