Jump to content

array_multisort()


smartalco

Recommended Posts

first, code:

$search_string = $_GET['search'];$results = array();$x = 1;foreach ($files as $direc) {	$contents = file_get_contents($direc);	$contents = strip_tags($contents);	$contents = strtolower($contents);	$score = substr_count($contents, $search_string);	if ($score != '0') {		$results[$x][0] = $direc;		$results[$x][1] = $score;	}	$x = $x + 1;}array_multisort($results[1], SORT_DESC, $results[0]);

my problem is... it isn't sorting, it is returning the exact same array... (except with missing array keys thrown onto the end with no values)if you can't tell from the code i want it to sort by the the 2nd part of each array entry, in descending orderwhat is my problem?

Link to comment
Share on other sites

No expert with array_multisort, but perhaps the issue comes with the fact that you're creating an array within the array. You're starting at iteration 1, and increasing it with every iteration of the for loop in which x is held. This means that $results could have many many more iterations that 0 and 1, and that the arrays in them are already sorted as-is, so there is nothing to sort for PHP. Perhaps this may be a bit more as to what you need(again,never really used array_multisort, but at least i try.)

$search_string = $_GET['search'];$results = array();foreach ($files as $direc) {$contents = file_get_contents($direc);$contents = strip_tags($contents);$contents = strtolower($contents);$score = substr_count($contents, $search_string);if ($score != '0') {$results[0][] = $direc;$results[1][] = $score;}}array_multisort($results[1], SORT_DESC, $results[0]);

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...