Jump to content

[SOLVED] sorting a 2 dimensional array


vampke

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.