Hello,
I'm running a PHP 4 script that is querying a MySQL server and returning field results. I'm populating a set of arrays like this:
while ($row = mysql_fetch_array($result)) {
$a[] = $row['id'];
$b[] = $row['name'];
$c[] = $row['category'];
}
and displaying the results further down the page. There is a need to sort by either name or category - right now, if the user chooses to sort by name or category, there is a requery of the database with an ORDER BY parameter. Ideally, we'd like to simply sort the array using rsort() or array_multisort() to prevent a re-query, but we're running into all sorts of trouble with the key associations not sorting like we hoped.
For the sake of example, if I put the arrays into something like:
$ar = $a,$b,$c;
and sort
array_multisort($ar[1],SORT_ASC)
The array sorts $a[] correctly, but breaks the correlation between $a and $b,$c ie. the 'name' field (for example) will sort, but the 'id' and 'category' fields do not sort to reflect the change to 'name'.
I tried the example on php.net's page for a similiar problem:
while ($row[] = mysql_fetch_array($result)){}
foreach ($row as $val) {
$sortarray[] = $val['name'];
}
array_multisort($rows, $sortarray);
but this never worked for me - the 'name' field never sorted at all.
If anyone has had any experience using array_multisort, or if they have a solution that might be better and skips this function, I'd appreciate any help they could give!
Sincerely,
gadhra (/s)