Krazifan
08-08-2002, 01:02 PM
Here is my problem. I’m trying to sort a multi-dimensional array by an array value in the second array. Here is an example of how I build the array structure (this is a simplified version of multiple selects that preclude me from just using an order by):
$result = sql_query(“select * from stats”, $dbi);
while($player = sql_fetch_array($result, $dbi)) {
$allplayer[] = $player;
}
This gives me a multi-dimensional array that should resemble my database table, correct? Now, I want to sort on an array value in $player. I’ve tried to get multisort and a few others working to no avail and I just decided to write my own bubblesort.
function multi_bs ($array1,$sortkey) {
global $done;
$done = 1 ;
while($done == 1) {
$done = 0;
for ($i = count($array1); $i > 0; $i--) {
if ($array1[$i][$sortkey] < $array1[$i-1][$sortkey]) {
$done = 1;
$holder = $array[$i] ;
$array[$i] = $array[$i-1];
$array[$i-1] = $holder;
}
}
}
return($array1);
}
The code above times out (infinite loop). Part of the problem is once the array gets into the bubblesort function, I can’t access the second array. It counts the number of values in array1 correctly (and passes it back to the calling function correctly), but I can’t access anything using $array1[#][#] or $array1[#][assoc.]. If I can fix that, I think it will solve my infinite loop.
Any ideas? Thanks.
$result = sql_query(“select * from stats”, $dbi);
while($player = sql_fetch_array($result, $dbi)) {
$allplayer[] = $player;
}
This gives me a multi-dimensional array that should resemble my database table, correct? Now, I want to sort on an array value in $player. I’ve tried to get multisort and a few others working to no avail and I just decided to write my own bubblesort.
function multi_bs ($array1,$sortkey) {
global $done;
$done = 1 ;
while($done == 1) {
$done = 0;
for ($i = count($array1); $i > 0; $i--) {
if ($array1[$i][$sortkey] < $array1[$i-1][$sortkey]) {
$done = 1;
$holder = $array[$i] ;
$array[$i] = $array[$i-1];
$array[$i-1] = $holder;
}
}
}
return($array1);
}
The code above times out (infinite loop). Part of the problem is once the array gets into the bubblesort function, I can’t access the second array. It counts the number of values in array1 correctly (and passes it back to the calling function correctly), but I can’t access anything using $array1[#][#] or $array1[#][assoc.]. If I can fix that, I think it will solve my infinite loop.
Any ideas? Thanks.