vampke Posted August 5, 2008 Share Posted August 5, 2008 hi guys, I'm using this simple polling script I found and adapted somewhere out there. The thing works just fine, but unfortunately it does not sort the results, which makes it rather useless for my needs. Could anyone help me with this? This is the function it uses for reading the results from a simple txt file: function readData(){ global $answers; $rawdata = file('polldata.txt'); $numberOfAnswers = sizeof($rawdata)-1; $count = 0; for ($i=0; $i <= $numberOfAnswers; $i++){ $answerData = explode(':',$rawdata[$i]); if (strlen(trim($answerData[0]))>0){ $answers[$count]['text'] = $answerData[0]; $answers[$count]['count'] = $answerData[1]; ++$count; } } } so what I want to do is sort it on $answerData[1] I tried asort($answers); foreach($answersas $key => $value) echo $key." ".$value." <br />"; but that returns 0 Array 1 Array 2 Array ... anyone? Link to comment https://forums.phpfreaks.com/topic/118303-solved-sorting-a-2-dimensional-array/ Share on other sites More sharing options...
.josh Posted August 5, 2008 Share Posted August 5, 2008 can you post results of echo "<pre>"; print_r($answers); echo "</pre>"; Link to comment https://forums.phpfreaks.com/topic/118303-solved-sorting-a-2-dimensional-array/#findComment-608799 Share on other sites More sharing options...
Barand Posted August 5, 2008 Share Posted August 5, 2008 function answersort($a, $b) { return $a['count'] - $b['count']; // (swap b and a for DESC sort) } usort ($answers, 'answersort'); Link to comment https://forums.phpfreaks.com/topic/118303-solved-sorting-a-2-dimensional-array/#findComment-608879 Share on other sites More sharing options...
vampke Posted August 12, 2008 Author Share Posted August 12, 2008 Thank you Barand, this works Link to comment https://forums.phpfreaks.com/topic/118303-solved-sorting-a-2-dimensional-array/#findComment-614530 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.